【问题标题】:Typescript: Object literal may only specify known properties打字稿:对象文字只能指定已知属性
【发布时间】: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


【解决方案1】:

我已将 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',
    }]
  }];
}

【讨论】:

    【解决方案2】:
    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"
            }]
        }];
    }
    

    看看它是否符合您的需求。 Playground

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-06
      • 1970-01-01
      • 2022-11-15
      • 1970-01-01
      • 2020-12-17
      • 2019-05-14
      • 2015-10-27
      • 1970-01-01
      相关资源
      最近更新 更多