当我尝试迭代从Map.values()返回的值时,Typescript会给我这个错误(其中Map的类型为< number,Foo>):
error TS2495: Type ‘IterableIterator<Foo>’ is not an array type or a string type.
根据ES6 doc,Map.values()应该返回一个Iterable而不是IterableIterator,我应该能够在for-for循环中使用它.
这在节点中工作正常:
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: 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 / core-js @ 0.9.34获取Map声明,所以我猜这个问题是在这个声明中?
其他版本& conf信息:
> 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" ] }
解决方法
我不理解tsc正在对tsconfig.json做一些隐式的事情.阅读
tsconfig.json doc让事情变得更加清晰:
首先,我正在编译这样的tsc mytypescript.ts(愚蠢地)导致Typescript默默地忽略tsconfig.json文件,这意味着它使用默认的ES5.但是这部分工作是因为tsc仍然找到了包含Map6,Iterable等ES6事件声明的core-js声明.这让我的调试有点麻烦.
其次,在获取Typescript实际接收我的配置后,无论如何配置都是错误的.我实际上并不需要或想要来自@ types / core-js的那些声明(非常肯定).是的,我会在我的项目中使用core-js作为polyfill,但是typescript / lib / lib.es6.d.ts中的ES6东西的Typescript comes with it’s own声明,以及@ types / core-js中的那些声明很古怪糟糕的东西……