【发布时间】:2019-12-03 17:47:08
【问题描述】:
我有一个代码:
// Library interfaces
interface PersonalData {
name: string;
age: number;
nick: string;
friends?: Friends[];
}
interface Friends extends PersonalData { }
// I want to add new props girls in friend interface
interface NewPropGirl {
girl: string;
friends?: NewPops[];
}
interface NewPops extends NewPropGirl { }
const test = (): Friends[] & NewPops[] => {
return [{
name: 'Oleg',
age: 25,
nick: 'yyy',
girl: 'Test',
friends: [{
name: 'Roman',
age: 22,
nick: 'bob',
girl: 'Test',
}]
}];
}
我想在库界面中添加一个新的界面参数girl。在这段代码中,我在第 31 行有一个错误
打字稿版本 3.5.3
Type '{ name: string; age: number; nick: string; girl: string; friends: { girl: string; }[]; }[]' is not assignable to type 'Friends[] & NewPops[]'.
Type '{ name: string; age: number; nick: string; girl: string; friends: { girl: string; }[]; }[]' is not assignable to type 'Friends[]'.
Type '{ name: string; age: number; nick: string; girl: string; friends: { girl: string; }[]; }' is not assignable to type 'Friends'.
Object literal may only specify known properties, and 'girl' does not exist in type 'Friends'.
游乐场链接:playground
【问题讨论】:
-
NewPropGirl应该派生自PersonelData。或者您必须将PersonalData上所有不存在的属性标记为可选。 -
@Eldar 我不能将 PersonalData 上所有不存在的属性标记为可选,因为它是来自 node_modules 的接口
-
哇,我有点困惑,但让我问你一些事情,你正在对接口进行循环依赖,所以据我了解,你的
friends?变量可能是PersonalData或 @987654329 @如果有错请纠正我
标签: javascript typescript