Skip to content

Instantly share code, notes, and snippets.

@gmotzespina
Last active March 24, 2020 13:09
Show Gist options
  • Select an option

  • Save gmotzespina/523134ee02c28c304ee9f6ba4ccacb58 to your computer and use it in GitHub Desktop.

Select an option

Save gmotzespina/523134ee02c28c304ee9f6ba4ccacb58 to your computer and use it in GitHub Desktop.
Generics Introduction: Sync function using generics
const rootUrl = "https://path_to_your_server/students";
class Student {
public sync: Sync<StudentProps> = new Sync<StudentProps>(rootUrl)
}
interface HasId {
id?: number;
}
class Sync<T extends HasId> {
constructor(public rootUrl: string) {}
save(data: T): void {
const { id } = data;
if (id) {
return axios.put(`${this.rootUrl}/id`, data);
}
return axios.post(this.rootUrl, data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment