【问题标题】:Interface incorrectly extends interface接口错误地扩展接口
【发布时间】:2018-11-28 06:29:55
【问题描述】:
class One {
  controls: { [key: string]: number };
}

interface Two<T> extends One {
  controls: { [key in keyof T]: number }
}

我收到一个错误:

TS2430:接口“二”错误地扩展了接口“一”。 属性“控件”的类型不兼容。 键入 '{ [key in keyof T]:数字; }' 不可分配给类型 '{ [key: string]: number; }'

在打字稿游乐场 (click) 中,一切似乎都还好。这是怎么回事?

打字稿版本:2.4.2

【问题讨论】:

    标签: typescript interface


    【解决方案1】:

    这是 2.4 中的一个问题,在 2.5 中得到了修复。你可以看看GitHub issue。基本上在 2.4 中这是不允许的,在 2.5 中是明确允许的:

    keyof T 上的映射类型 [是] 具有字符串索引签名的类型的子类型

    【讨论】:

      猜你喜欢
      • 2019-10-14
      • 2022-11-10
      • 2017-07-10
      • 2019-03-21
      • 2020-11-29
      • 2020-06-03
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多