9

Typescript is giving me this error when I try to iterate 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 the ES6 doc Map.values() should return an Iterable not an IterableIterator and I should be able to use it in a for-of 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 the 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'm getting Map declarations from @types/[email protected] so I guess the issue is in this declaration??

Other version & conf info:

> 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"
  ]
}

1 Answer 1

2

I wasn't understanding some of the implicit stuff tsc is doing rel the tsconfig.json. Reading tsconfig.json doc made things clearer:

First, I was transpiling like this tsc mytypescript.ts which (stupidly) causes Typescript to silently ignore tsconfig.json file, which meant it was using the default ES5. But this was partially working because tsc was still finding the core-js declarations which contain declaration for ES6 things like Map, Iterable etc. That threw my debugging a bit.

Second, after getting Typescript to actually pick up my config, the config was wrong anyway. I don't actually need or want those declarations from @types/core-js (pretty sure). Yes, I'll use core-js as a polyfill in my project, but Typescript comes with it's own declarations for ES6 stuff in typescript/lib/lib.es6.d.ts, and the ones in @types/core-js are old weird and crappy or something ...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.