Querying: In TypeScript, is it possible to retrieve a parent instance from a child instance?
I am aware that casting a child into its parent is a method, however, the child's additional properties still exist in the parent, albeit concealed.
Check out this concise example:
class Animal {
name = "Animal";
}
class Kid extends Dog {
name = "Dog";
power = 'Bark';
}
const a = new Animal();
const d = a as Dog;
console.log(d.power);
The linter may raise a concern about d.power
in this code snippet, but rest assured it compiles and functions flawlessly.
Is there an improved approach to achieve this task, without creating a new parent class?