Before angular's new HttpClient was introduced, our objects returned from the http api call could be validated with the instanceof
keyword. they no longer can with the HttpClient Module. I'm trying some simple methods but the type checks return false every time. the desired behavior of:
```
getCow() {
return this.http.get<Cow>(ApiRoute.GET_COW, options)
.map(res => res as Cow)
.toPromise()
.then((c: Cow) => {
console.log(c instanceof Cow); //this is false
})
}
```
would return true. does anyone know of a simple way to new up an instance behind the scenes of the http client?
new
and initialize withcow
props. It's not something that Angular or TS can handle for you. – VermouthCow
is a class with no properties and the default constructor. – Prescindres
contain then? Does this mean that it can be discarded? – VermouthCow
is a class with one property,public sound: string;
and res is an object with one property, a string called sound equal to 'moo'. I guess in the end I'm asking why duck typing fails me here after saying its a cow. After all it looks and sounds like a cow. – Prescindinstanceof
does a specific thing, it checks if object prototype chain descends fromCow
, basicallycow.__proto__ === Cow.prototype
. It's an opposite of duck typing (cow typing?) – Vermouth