Cast not casting

Viewed 1397

I made a simple Http call:

http.get('/test/data/user.json').
    map(res => <User> res.json()).
    subscribe(user => {
        console.log(typeof user);
        console.log(user);
        console.log(user.getName());
});

with the following data in user.json:

{
    "name":"Robert"
}

and the following User class:

export class User{
    name:string;
    lastName:string;
    getName():string{
        return this.name;
    }
}

Problem is the result I get in console:

console.log(typeof user);

object

console.log(user);

Object {name: "Robert"}

console.log(user.getName());

Uncaught TypeError: user.getName is not a function

Problem

Problem here is that my object is an object, not a User, so it does not have all properties, and it has no methods.

Also tried with Promise-based call and user.json() as User instead of <User> res.json()

EDIT: not similar to casting to an interface since interface doesn't have its own behaviour.

1 Answers
Related