typescript - 为什么长度应该是索引器的子类型?

标签 typescript typescript1.4

我在 TypeScript 手册中看到一个示例,地址为 here 。 我不明白为什么 length 属性无效?

interface Dictionary {
  [index: string]: string;
  length: number;    // error, the type of 'length' is not a subtype of the indexer
} 

最佳答案

因为x['length']是一个数字,但你说索引x字符串会产生一个字符串。它们不可能都是真的。

关于typescript - 为什么长度应该是索引器的子类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29857144/

相关文章:

angular - 如何将服务注入(inject)函数?

typescript - 如何在 typescript 中创建静态 map ?

angularjs - Angular Js+ typescript : How to create a Dynamic table

node.js - 得到错误 TS2304 : Cannot find name 'Buffer'

angularjs - IIS 7.5 中不允许使用 Type Script + Angular Js + Web API Status 405 的方法

javascript - 无法在模块中找到 'ng'... - 损坏的 Intellisense?

javascript - 本地库的 Webpack 外部配置

javascript - formGroup 需要一个 FormGroup 实例。请在我在其他表单组中创建表单组时传入一个

javascript - TS2339 : Property 'style' does not exist on type 'Element'