Last active
March 24, 2020 13:09
-
-
Save gmotzespina/523134ee02c28c304ee9f6ba4ccacb58 to your computer and use it in GitHub Desktop.
Generics Introduction: Sync function using generics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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