为什么这在Typescript 中不起作用?
class Parent {
id: string = ''
}
class Child extends Parent{
name: string = ''
}
const fails: (created: Parent) => void = (created: Child) => { return };
const failsToo: ({ created }: { created: Parent }) => void = ({ created }: { created: Child }) => { return };
至少对我来说,这个错误非常奇怪:
Type '(created: Child) => void' is not assignable to type '(created: Parent) => void'.
Types of parameters 'created' and 'created' are incompatible.
Property 'name' is missing in type 'Parent' but required in type 'Child'
它看起来像是在try assign a Parent to a Child,但在真正的代码中是向后的(正在try assign a method parameter that is a Child to a Parent.这是有意义的,因为子代是父代的超集)
我是不是遗漏了什么?