我在 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/