Typescript: Map.values ​​() error providing IterableIterator not Iterable

Typescript gives me this error when I try to Map.values() over the value returned from Map.values() (where the map has type <number, Foo>):

error TS2495: type 'IterableIterator <Foo>' is not an array type or a string type.

According to ES6, doc Map.values() should return an Iterable not an IterableIterator, and I should be able to use it in for a loop.

This works fine in 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) } 

This gives an error from 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) } 

I get Map declarations from @types/ core-js@0.9.34 , so I think the problem is this declaration

Other versions and config:

 > 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" ] } 
+4
source share
1 answer

I did not understand that some implicit tsc doing rel tsconfig.json . Reading tsconfig.json doc made everything more clear:

Firstly, I broadcast as tsc mytypescript.ts , which (stupidly) causes Typescript to silently ignore the tsconfig.json file, which means that it used ES5 by default. But this partially worked, because tsc still found core-js declarations containing ads for ES6, such as Map, Iterable, etc. This threw off my debugging a bit.

Secondly, after receiving Typescript, to really get my config, the configuration was wrong anyway. I really don't need or don't need those ads from @types/core-js (pretty sure). Yes, I will use core-js as a polyfill in my project, but Typescript contains native declarations for ES6 in typescript/lib/lib.es6.d.ts and those of @types/core-js are old weird and crappy or something- then ...

0
source

Source: https://habr.com/ru/post/1260501/


All Articles