Question: I have encountered an issue with Casting in Typescript.
In a specific Use Case, I casted an object to a class with a method. However, when I attempted to call this method later on, it returned as undefined. See the example below for reference:
export class Test {
property1: any;
property2: any;
constructor(){}
sayHello(): string {
return 'hello';
}
}
testData = {
property1: '',
property2: 2
} as Test;
testData.sayHello(); <-- undefined
I have also created a Angular application demo on stackblitz where you can see this behavior: https://stackblitz.com/edit/angular-y3s9r4
Can anyone provide insights into why this is happening? And is there a way to inherit
methods as well?