Skip to content

Instantly share code, notes, and snippets.

@joanllenas
Last active March 5, 2017 00:59
Show Gist options
  • Save joanllenas/3cfeabb5a1a0edb21f6baa7ad43189ea to your computer and use it in GitHub Desktop.
Save joanllenas/3cfeabb5a1a0edb21f6baa7ad43189ea to your computer and use it in GitHub Desktop.
TS type inference 2
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
});
export interface User {
id: number;
name: string;
}
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