I'm trying to call a function from an object in my react state
//currentUser is of type User
this.state.currentUser.rankUp()
but it just gives an error this.state.currentUser.rankUp is not a function. If I create a new user object though it does let me call said function.Even if I try casting it as user: var user:User = this.state.currentUser as User then calling the function it gives the same error. I tried printing the type and even when casting it it returns as object not User
The only way I've managed to get it to work so far is user.rankUp = (new User("a")).rankUp; which feels like its incorrect even though it works.
EDIT:
User.rankUp is defined as:
rankUp(){
this.rank +=1;
}
as Thingis a hint, not a conversion, it just tells TS you know what the type of the value will be (and, in this case, you were wrong). The solution you have is nearly correct, TypeScript won't convert a generic object (e.g. from JSON) to an instance without you newing one up. But you don't have to patch the one thing, tryuser = new User(user.whatever);instead.rankUpdefined?