custom created method error: "is not a function" custom created method error: "is not a function" typescript typescript

custom created method error: "is not a function"


If you cast JSON to a TypeScript class, all is happening is that you indicate to the static analysis it can safely assume the value is of that class; that doesn't actually change the JSON value at all (i.e it doesn't make it an instance of that class).

What you want is probably How do I cast a JSON object to a typescript class or Cast JSON object to TypeScript class instance


Your service most probably only returns plain objects, meaning they don't have the member methods of the Hero class.

You need to explicitly create new Hero() objects to have the toggleState() method available on the hero object.