Property ‘move’ is missing in type ‘Employee’ but required in type ‘Animal’.ts(2741)
test.ts(89, 3): ‘move’ is declared here.
class Animal {
private name: string
constructor(name: string) {
this.name = name
}
move(distance: number = 0) {
console.log(`$(this.name) moved ${distance}m`)
}
}
class Rhino extends Animal {
constructor() {
super('Rhino')
}
}
class Employee {
private name: string
constructor(name: string) {
this.name = name
}
}
let animal = new Animal('Goat')
let rhino = new Rhino()
let employee = new Employee('Bob')
animal = rhino
animal = employee
也就是说 某个变量具有了某个类型之后 不能轻易改变其类型了吗?
还是要判断类型是否兼容?
父类型 一定兼容子类类型 animal = rhino
子类型不一定兼容父类型 rhino = animal (是不是要做个类型diff啊,有啥原则吗)
animal = employee 这个就提示
Type ‘Employee’ is not assignable to type ‘Animal’.
Types have separate declarations of a private property ‘name’.
理解起来还是很别扭。。
登录后可查看更多问答,登录/注册