27

TS throws strange error:

Error:(125, 18) TS2569: Type 'string' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.

How comes a string is not a string?

I want to see how TS is going to compile spread operator for a string.

My code in browser console. A string is broken up into characters:

> s = 'abcdef';
> r = [...s];
< (6) ["a", "b", "c", "d", "e", "f"]

My code in TS:

const s: string = 'abcdef';
const res = [...s]; // <= Error: Type 'string' is not an array type or a string type
console.log(res);

Why?

TS version:

  "dependencies": {
    "typescript": "^3.5.3"
  }

UPD:

@VtoCorleone A screenshot enter image description here

UPD:

My tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "downlevelIteration": false,
    "allowJs": true,
    "skipLibCheck": false,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "alwaysStrict": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": false,
    "noEmit": false,
    "sourceMap": true,
    "baseUrl": "./",
    "jsx": "preserve"
  },
  "compileOnSave": true,
  "files": [
    "sample.ts"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ]
}

7
  • 5
    Might be a stupid question, but have you tried adding "downlevelIteration": true to your tsconfig? Commented Jul 11, 2019 at 13:45
  • @OliverRadini, Yes, I did both, true and false. TS doesn't blow when true as expected. But why is it complaining a string is not a string? Commented Jul 11, 2019 at 13:51
  • 2
    I just copy and pasted your exact example with TS 3.5.1 and got no warnings or errors. Commented Jul 11, 2019 at 14:35
  • 1
    What are you targeting? In the TS playground I get errors if I target ES5 or below... Commented Jul 11, 2019 at 16:11
  • 1
    @HereticMonkey Yes, indeed "target": "es5",. I've updated the question with my tsconfig.json Commented Jul 12, 2019 at 8:44

2 Answers 2

31

To expand on Heretic Monkey's comment:

Changing target from es5 to es2015 or es6 fixes the issue. Here's my full tsconfig.json for clarity:

{
  "compilerOptions": {
    "target": "es2015",
    "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",
  },
  "exclude": [
    "node_modules"
  ],
  "include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx"
  ]
}

Working example

Side note: "downlevelIteration": true also fixed it, but that doesn't seem like the correct solution to me.

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

Comments

4

I have Angular-CLI (8) - in my case modification tsconfig.json in key "target": "es2015" (or es2018) not helps at all. Only adding "downlevelIteration": true helps. Here is my whole file

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "esnext",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "downlevelIteration": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  }
}

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.