当我尝试迭代返回的值时,Typescript 给了我这个错误Map.values()
(其中 Map 的类型为 ):
错误 TS2495:类型“IterableIterator”不是数组类型或字符串类型。
根据 ES6doc Map.values()
应该返回一个Iterable
不是 IterableIterator,我应该能够在 for-of 循环中使用它。
这在以下情况下效果很好node
:
var data = [
{id: 0},
{id: 1},
{id: 3}
];
var m = new Map(data.map(n => [n.id,n]));
for(var i of m.values()) { console.log(i) }
这给出了错误tsc
:
interface Foo {
id: number;
}
var data: Foo[] = [
{id: 0},
{id: 1},
{id: 2}
];
var m = new Map<number,Foo>(data.map(n => <[number,Foo]>[n.id,n]));
for(var i of m.values()) { console.log(i) }
我收到来自以下位置的地图声明@types/[email protected]
所以我想问题出在这个声明中?
其他版本和配置信息:
> tsc --version
Version 2.0.3
> cat tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true,
"typeRoots": [
"./node_modules/@types/"
]
},
"compileOnSave": true,
"exclude": [
"node_modules/*",
"**/*-aot.ts"
]
}