Scenario
I know the shape of the data that is returning from an API call, and I want it to have some extended functionality via a TypeScript class. I already know that I can do the following to fulfill this requirement:
this.http
.get(url)
.map((res: Response) => {
return res.json().map((obj: Type) => {
return Object.assign(new Type(), obj);
});
})
// And so on
Would it be possible to make a generic implementation of this similar to the following:
get<T>(url: string): Observable<T> {
return this.http
.get(url)
.map((res: Response) => {
return res.json().map((obj: T) => {
return Object.assign(new T(), obj); // not sure how to approach this
});
});
}
The above works for returning a straight up JavaScript Object, but will I'm not sure how to run assign on a generic type.