初始化 TypeScript 数组接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15055972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Initializing a TypeScript array interface
提问by oz1cz
Why is this illegal in TypeScript?
为什么这在 TypeScript 中是非法的?
interface numarr {
[i : number] : number;
}
var p : numarr = [3,6,8];
The compiler says "Cannot convert 'number[]' to 'numarr'."
编译器说“无法将 'number[]' 转换为 'numarr'。”
I'm afraid I've misunderstood something quite basic here. I thought the point of the above interface was to describe an array of numbers indexed by numbers, which is exactly what [3,6,8] is.
恐怕我在这里误解了一些非常基本的东西。我认为上述接口的重点是描述一个由数字索引的数字数组,这正是 [3,6,8] 的意思。
采纳答案by oz1cz
I asked this question in 2013. It is no longer relevant. The code in my question is now accepted by the compiler.
我在 2013 年问过这个问题。它不再相关。我的问题中的代码现在被编译器接受。
回答by Serkan Inci
You can alternatively use the following syntax, if I didn't get you wrong.
如果我没有弄错的话,您也可以使用以下语法。
var arr : number[] = [3, 6, 8];