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. In this code, I have an Error on line 31
TYPESCRIPT VERSION 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 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. In this code, I have an Error on line 31
TYPESCRIPT VERSION 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
Share Improve this question edited Dec 3, 2019 at 17:56 Oleg Ovcharenko asked Dec 3, 2019 at 17:47 Oleg OvcharenkoOleg Ovcharenko 5372 gold badges8 silver badges19 bronze badges 3-
NewPropGirl
should be derived fromPersonelData
. Or you have to mark all none existing properties onPersonalData
as optional. – Eldar Commented Dec 3, 2019 at 17:53 - @Eldar I can't mark all none existing properties on PersonalData as optional because it is interface from node_modules – Oleg Ovcharenko Commented Dec 3, 2019 at 17:59
-
Woah am a bit confused, but let me ask you something, you are putting cyclic dependency of interfaces, so from what i understand is your
friends?
variable could either be aPersonalData
orNewPropGirl
correct me if am wrong – pavan kumar Commented Dec 3, 2019 at 18:16
2 Answers
Reset to default 2I have extended PersonalData to NewPropGirl to let your result hold an extra field and made that as the return type
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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744248423a4565048.html
评论列表(0条)