I defined an applyDamage method to accept number or string, but implemented it with only with a number argument. When I call this method using base class, I have incorrect behavior. Why doesn't TypeScript show an error?
interface Character {
applyDamage(value : number): number;
}
interface Humanoid extends Character {
hp:number;
applyDamage(value: number|string): number
}
class Monster implements Humanoid {
hp:number = 10;
applyDamage(v: number) {
this.hp -= v;
return v;
}
}
const monster:Humanoid = new Monster();
monster.applyDamage("hello");
console.log(monster.hp); // <-- NaN
const monster:Humanoid = new Character();, you will get an error when trying to callapplyDamagewith astring