相关疑难解决方法(0)

TS 错误:类型“字符串”不是数组类型或字符串类型。字符串怎么不是字符串?

TS 抛出奇怪的错误:

错误:(125, 18) TS2569:类型“字符串”不是数组类型或字符串类型。使用编译器选项“--downlevelIteration”来允许迭代器迭代。

为什么字符串不是字符串?

我想看看 TS 将如何为字符串编译扩展运算符。

我在浏览器控制台中的代码。一个字符串被分解成字符:

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

我在 TS 中的代码:

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

为什么?

TS版本:

  "dependencies": {
    "typescript": "^3.5.3"
  }
Run Code Online (Sandbox Code Playgroud)

更新:

@VtoCorleone 截图 在此处输入图片说明

更新:

我的 tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "downlevelIteration": false,
    "allowJs": …
Run Code Online (Sandbox Code Playgroud)

typescript spread-syntax

15
推荐指数
1
解决办法
1万
查看次数

标签 统计

spread-syntax ×1

typescript ×1