在TypeScript中,我有一个数组,我像使用一个map一样来访问另一个对象数组。这是我的代码的一个非常简化的例子:
var mp : Number[] = [1, 2, 0];
var arr : any[] = ['a', 4, /regex/];
console.log(arr[mp[2]])
然而,运行这个会产生:
error TS2538: Type 'Number' cannot be used as an index type.
我已经在其他地方查看了,但这种错误的其他答案并不适用于像数组和Number
这样简单的东西。如果不是一个Number
对象,我怎么能索引一个数组呢?
typeof(1)
is not necessary, justtypeof 1
is most commonly used. It's unnecessary parentheses making it look like a function call, just like typingreturn(1)
- Ruan Mendes