[英]Typescript: Object literal may only specify known properties
我有一個代碼:
// 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'.
游樂場鏈接: 游樂場
我已將 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"
}]
}];
}
看看它是否符合您的需求。 操場
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.