【问题标题】:Are object index signatures are equivalent to array types?对象索引签名是否等同于数组类型?
【发布时间】:2012-11-12 08:41:57
【问题描述】:

规范的第 3.5.4 节指出:“ElementType[] 形式的数组类型等同于具有索引签名 [index: number]: ElementType 的对象类型”,但情况似乎并非如此,例如

var a: {[index: number]: string;};
var b: string[];
a = ['1','2'];  // ERROR: Cannot convert 'string[]' to '{ [index: number]: string; }'
b = ['1','2'];  // OK

我在这里错过了什么?

【问题讨论】:

标签: typescript


【解决方案1】:

是的,这是编译器中的一个已知错误。它将在未来的版本中修复。最好的解决方法是强制转换(在任务的两侧 - a = <string[]>['1', '2'] 可能看起来不那么奇怪)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 2016-01-25
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多