Typescript: IterableIterator errors on Arrays

Created on 12 Mar 2017  ·  2Comments  ·  Source: microsoft/TypeScript



TypeScript Version: 2.2.1

Code

var arr = ['a', 'b', 'c'];
for (let entry of arr.entries()) {
  console.log(entry);
}
// [0, 'a']
// [1, 'b']
// [2, 'c']

for (let idx of arr.keys()) {
    console.log(idx);
}
// [0]
// [1]
// [2]

for (let val of arr.values()) {
    console.log(val);
}
// ['a']
// ['b']
// ['c']

Tsconfig:

{
  "compilerOptions": {
    "module": "es2015",
    "target": "es5",
    "moduleResolution": "node",
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noUnusedLocals": true,
    "sourceMap": true,
    "experimentalDecorators": true,
    "jsx": "react",
    "jsxFactory": "h",
    "noEmitHelpers": true,
    "importHelpers": true,
    "pretty": true,
    "outDir": "ts-output",
    "lib": [
      "dom",
      "es2015"
    ]
  }
}

Expected behavior:
No errors => this is es2015 standard
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keys
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values

Actual behavior:
entries

[ts] Type 'IterableIterator<[number, string]>' is not an array type or a string type.

keys

[ts] Type 'IterableIterator' is not an array type or a string type.

values

[ts] Type 'IterableIterator' is not an array type or a string type.

Question

Most helpful comment

@aluanhaddad thanks! How do you know about these things before I do 😕

All 2 comments

The emit for this feature when targeting ES < 2015 is available in the nightly build
npm i -g typescript@next
It requires that you specify the new compiler option --downlevelIteration.

@aluanhaddad thanks! How do you know about these things before I do 😕

Was this page helpful?
0 / 5 - 0 ratings