Converting httpClient answer to model objects [Angular 6]
When doing that, TypeScript only does "type assertion". It means that you're telling TypeScript that your object is of type MyClass
, but the object isn't actually an instance of MyClass
at runtime. In order to call functions defined in your model object, you have to define constructors in your model classes like that :
constructor(obj?: any) {
Object.assign(this, obj);
}
Then in your services add a mapping like this :
http.get<MyClass>('/my-class').pipe(
map(res => new MyClass(res))
Note: the code above is RxJS 6 style, i don't know which version you are using