如何在打字稿中“接口”一个接口?

时间:2020-08-01 16:33:50

标签: typescript

我有一个基本接口,我希望我的子接口看起来像基本接口。 我该怎么办?

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  };
}

我尝试使用type ChildInterface = BaseInterface,但是那时我无法自定义我的ChildInterface

谢谢

1 个答案:

答案 0 :(得分:0)

Use the extends keyword.

interface ChildInterface extends BaseInterface {
    someOtherProp:string;
}

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