I'm using TypeScript to build an app and I'm making API calls to retrieve objects. For instance, I have a TypeScript User
Object like this:
export class User {
id : number;
name : string;
email : string;
}
And my API returns
{
"id" : 3,
"name" : "Jonn",
"email" : "john@example.com"
}
I want to convert that JSON to a User
. I've read in another posts I can do this:
let user : User = <User> myJson;
This seemly works. I can access properties of the user like user.name
but my problem is that, if the User
class implements some method, the properties are not available. For example, if inside the User
class I have this:
getUppercaseName() : string {
return this.name.toUppercase();
}
This happens:
user.name
returns John
but user.getUppercaseName()
returns undefined
What's going on? How to solve this