简体   繁体   English

如何“接口” typescript 中的接口?

[英]How to “interface” an interface in typescript?

I've got a base interface and I want my child interface to look like the base interface.我有一个基本界面,我希望我的子界面看起来像基本界面。 How can I do so?我该怎么做?

interface BaseInterface {
  api: { [key: string]: string  };
  ui: { [key: string]: string  };
}

interface ChildInterface {
  // I want to force this interface to have the same shape as BaseInterface
  // like so :

  api: { firstName: string  };
  ui: { firstName: string  };
}

I tried with type ChildInterface = BaseInterface but I cannot manage to custom my ChildInterface then.我尝试使用type ChildInterface = BaseInterface但我无法设法自定义我的ChildInterface

Thanks谢谢

Use the extends keyword. 使用extends关键字。

interface ChildInterface extends BaseInterface {
    someOtherProp:string;
}

const someItem : ChildInterface = //...
console.log(someItem.api.firstName);
console.log(someItem.someOtherProp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM