【发布时间】:2021-12-21 09:25:34
【问题描述】:
这是我的问题的一个最小示例
interface Dog {
legs: number;
}
interface Person {
arms: number;
}
type Living = Person | Dog;
interface Speaker<T extends Living> {
speak: (living: T) => void;
};
const michel: Speaker<Person> = { speak: (person) => console.log(`I have ${person.arms} arms`) };
const speakers: Array<Speaker<Living>> = [michel];
它抛出这个错误
Type 'Speaker<Person>' is not assignable to type 'Speaker<Living>'.
Type 'Living' is not assignable to type 'Person'.
Property 'harms' is missing in type 'Dog' but required in type 'Person'
我想要一个Speaker 数组,它接受任何类型的Living。
感谢您的宝贵时间!
【问题讨论】:
标签: javascript arrays typescript typescript-generics