Lets suppose we have a rest api at this url /api/stuffs/
where we can get a list of Stuff
.
here is the code to http get the list of Stuff
:
getStuffs(): Observable<Stuff[]> {
return this.http.get(this.url)
.map(this.extractStuffs)
.catch(this.handleError);
}
private extractStuffs(res: Response) {
let stuffs = res.json() as Stuff[];
return stuffs || new Array<Stuff>();
}
everything works fine with this code, except the fact that the stuffs
array in the extractStuffs
function is not an array of Stuff
but and array of Object
instead, even if the signature of the function is Observable<Stuff[]>
and the result from the api is casted to Stuff[]
. What's weird is that typescript compiler is not throwing any error (even if result type is different from signature), witch is totally normal if we take a look at the generated JS file :
StuffService.prototype.extractStuffs = function (res) {
var stuffs = res.json();
return stuffs || new Array();
};
So obviously casting the result into Stuff[]
is not even considered by the compiler.
Is there any way to cast it properly without doing it manually ?
Thanks.