[英]Typescript: Object literal may only specify known properties
I have a code:我有一个代码:
// 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',
}]
}];
}
I want to add a new interface parameter girl to the library interface.我想在库界面中添加一个新的界面参数girl 。 In this code, I have an Error on line 31
在这段代码中,我在第 31 行有一个错误
TYPESCRIPT VERSION 3.5.3打字稿版本 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 link: playground游乐场链接: 游乐场
I have extended PersonalData to NewPropGirl to let your result hold an extra field and made that as the return type我已将 PersonalData 扩展到 NewPropGirl 以让您的结果包含一个额外的字段并将其作为返回类型
interface PersonalData {
name: string;
age: number;
nick: string;
friends ? : Friends[];
}
interface Friends extends PersonalData {}
// I want to add new props
interface NewPropGirl extends PersonalData {
girl: string;
friends ? : NewPops[];
}
interface NewPops extends NewPropGirl {}
const test = (): NewPops[] => {
return [{
name: 'Oleg',
age: 25,
nick: 'yyy',
girl: 'Test',
friends: [{
name: 'Roman',
age: 22,
nick: 'bob',
girl: 'Test',
}]
}];
}
interface PersonalData {
name: string;
age: number;
nick: string;
friends?: Friends[];
}
interface Friends extends PersonalData { }
// I want to add new props
interface NewPropGirl extends PersonalData {
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:"Bella"
}]
}];
}
See if it fit your needs.看看它是否符合您的需求。 Playground
操场
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.