继承接口时如何向对象添加属性

时间:2019-09-13 17:26:53

标签: typescript

总结一下我的问题,我有一个接口,用户带有一个填充了属性的“属性”对象。我有另一个接口SpecialUser,其中的“属性”对象需要包含User上不存在的属性。当前,新属性对象将覆盖旧属性对象。我可以通过从父界面粘贴所有属性,然后附加后面的属性来使其工作,但这不是最理想的解决方案。

export interface User{
  attributes: {
     full_name: string;
  }
}

export interface SpecialUser extends User {
  attributes: {
     occupation: string;
  }
}

我想要的是SpecialUser的'attributes'包含用户的'attributes'上的字段以及新属性(因此它将同时包含full_name和职业)。实际结果已完全覆盖。

2 个答案:

答案 0 :(得分:2)

一种选择是使用交叉点类型。

unstack(df, y~ ave(x, x, FUN = seq_along))

TypeScript Playground

答案 1 :(得分:0)

这样适合您的用例吗?

export interface User{
  attributes: {
     full_name?: string;
     occupation?: string;
  }
}

或者因为您实际上是在修改属性

export interface Attributes{
    full_name: string;
}

export interface SpecialAttributes extends Attributes {
    //has all other properties of original attributes (i.e. full_name)
    occupation: string;
}

export interface User{
    attributes: Attributes
}

export interface SpecialUser extends User {
    attributes: SpecialAttributes //overrides original attributes
}