首页 > 解决方案 > 如何扩展 TypeScript 接口并对其进行非部分化

问题描述

我希望能够做到以下几点:

interface Partials {
  readonly start?: number;
  readonly end?: number;
}

interface NotPartials extends Partials /*incorporate Unpartialing somehow */ {
  readonly somewhere: number;
}

那么,NotPartials 将是:

readonly start: number;
readonly end: number;
readonly somewhere: number;

注意现在需要 start 和 end 。这可能吗?

标签: javascripttypescript

解决方案


好吧,这很愚蠢。

我认为在 TypeScript 2.8 中这是可能的:

interface Partials {
  readonly start?: number;
  readonly end?: number;
}

interface NotPartials extends Required<Partials> {
  readonly somewhere: number;
}

推荐阅读