I have a class at type script:
export class Child {
name:string;
age:number;
}
I want to force class instances to have only properties
What we want:
Solution:
class Animal {
name: string = 'default value';
group: string = 'default value';
constructor(data: Partial = {}) {
Object.assign(this, data)
}
echo() {
return `My name is ${this.name}, I'm from: ${this.group}`;
}
}
class Dog extends Animal {
echo() {
return super.echo() + ' from Dog class';
}
}
const dog = new Dog({name: 'Teddy'});
console.log(dog.echo());
Animal - root class
Dog - nested class
All works without typescript errors