Method in typescript class give error “is not a function”

as User only tells the compiler that it’s safe to assume that the value is of type User, but doesn’t have any effect on runtime and it won’t have any methods because methods are not passed with JSON.

You would need

let user = new User(JSON.parse(sessionStorage.getItem("User")));

to get an actual User instance. You would need to create a constructor that assigns the values from JSON to fields like

class User {
  ...
  constructor(json:any) {
    this.firstName = json.firstName;
    this.lastName = json.lastName;
  }

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)