Given the following classes
class A {
constructor(){
this.name ="Some name";
}
name:string;
}
class C extends A {
get Age() {
return 21;
}
SayHello(){
return "Hello";
}
}
If you would to build the OBJECT from a JSON Parse
var demo2:C= JSON.parse('{"name":"Joan of Arc"}') AS C;
This will not work:
console.log(demo2.Age);
console.log(demo2.SayHello());
Any workaround or ideas?
as(or<>syntax) is a type assertion. You just override the compiler and tell it "regardless of what you think this thing is, not consider it this other thing. Just trust me". That doesn't actually change anything about the object, simply stops compilation errors, not runtime ones. So, if you have object of one shape and want it in another, you have to change it yourself. With classes the usual way is to accept an object in the constructor or otherwise initialise the class instance via an object.