Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'IterableIterator<number>' is not an array type or a string type

I am getting the following error when trying to generate a dynamic range of numbers.

Type 'IterableIterator<number>' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.

I found an existing question but the solution isn't working for me.

TypeScript and Iterator: Type 'IterableIterator<T>' is not an array type

My setup is a follows...

  • I used the command npx create-next-app@latest --ts
class FlexGrid extends React.Component {

    range(size: number, startAt = 0) {
        return [...Array(size).keys()].map(i => i + startAt);
    }

I have tried setting target: es6 and downlevelIteration: true neither resolve the error.

{
  "compilerOptions": {
    "target": "es6",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "downlevelIteration": true
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}

like image 222
SoonGuy Avatar asked Oct 23 '25 02:10

SoonGuy


1 Answers

Apparently it is being strict and doesn't want to create an array using spread from an iterator (which is not an array). However it's possible to create an array from an iterator using the Array.from function:

range(size: number, startAt = 0) {
  return Array.from(Array(size).keys()).map((i) => i + startAt);
}

Array.from accepts Iterable.

like image 138
tromgy Avatar answered Oct 26 '25 05:10

tromgy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!