I have a class Animal with some methods, and an extension of the class - called Bird - that overwrites one of its parent methods to handle an edgecase.
It looks like this:
class Animal {
constructor(animal) {
this.animal = animal;
}
public define(){
return {name: this.animal.name}
}
}
class Bird extends Animal{
public define(){
return {color: this.animal.color}
}
}
Calling bird.define() works fine. But Typescript goes mad, claiming:
Property 'define' in type 'Bird' is not assignable to the same property in base type 'Animal'. Type '() => { color: string; }' is not assignable to type '() => { name: string }'. Type '{ color: string; }' is missing the following properties from type '{ name: string; }':
name
How to fix this except doing a ts-ignore?
Birdis not anAnimalif itsdefinemethod returns something different. You will just have to rethink your software architecture a bit to determine what works best for you. Perhaps class hierarchies /extendsare not needed here, and you can use some other form of JavaScript inheritance such as augmentation.