Last active
March 5, 2017 00:59
-
-
Save joanllenas/3cfeabb5a1a0edb21f6baa7ad43189ea to your computer and use it in GitHub Desktop.
TS type inference 2
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 userService = new HttpService<User>(); | |
| const user = { id: 1, name: '' }; | |
| userService.save(user) // user type is inferred to be User | |
| .then(resp => { // resp type is inferred to be HttpResponse | |
| console.log(resp.data.name); // resp.data is inferred to be User | |
| }); |
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
| export interface User { | |
| id: number; | |
| name: string; | |
| } |
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
| export interface HttpResponse<T> { | |
| data: T; | |
| } | |
| export class HttpService<T> { | |
| save(data: T): Promise<HttpResponse<T>> { | |
| return Promise.resolve({data}); | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment