我希望以下代码无法编译。原因是当将person
和expert
分配给people
时,person
常数满足people
常数的类型,该常数应为Person
类型。但是,expert
具有skills
类型不存在的Person
文件。
但是它确实可以编译。
interface Person {
name: string;
age?: number;
}
interface Developer {
name: string;
age?: number;
skills: string[];
}
const person: Person = {
name: 'john doe',
age: 20,
};
const expert: Developer = {
name: 'jane doe',
skills: ['javascript', 'react']
};
const people: Person[] = [ person, expert ];
console.log(people);