javascript - Typescript: Object literal may only specify known properties - Stack Overflow

I have a code: Library interfacesinterface PersonalData {name: string;age: number;nick: string;frien

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 from PersonelData. Or you have to mark all none existing properties on PersonalData 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 a PersonalData or NewPropGirl correct me if am wrong – pavan kumar Commented Dec 3, 2019 at 18:16
Add a ment  | 

2 Answers 2

Reset to default 2

I 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信