【问题标题】:TS Error: Type 'string' is not an array type or a string type. How a string is not a string?TS 错误:类型“字符串”不是数组类型或字符串类型。字符串怎么不是字符串?
【发布时间】:2019-11-21 05:47:44
【问题描述】:

TS 抛出奇怪的错误:

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

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

我想看看 TS 是如何为字符串编译展开运算符的。

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

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

我在 TS 中的代码:

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

为什么?

TS 版本:

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

更新:

@VtoCorleone 截图

更新:

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

【问题讨论】:

  • 可能是个愚蠢的问题,但您是否尝试过将"downlevelIteration": true 添加到您的tsconfig 中?
  • @OliverRadini,是的,我都做了,truefalse。当true 符合预期时,TS 不会爆炸。但是为什么它抱怨一个字符串不是字符串呢?
  • 我只是用 TS 3.5.1 复制并粘贴了您的确切示例,没有收到任何警告或错误。
  • 您的目标是什么?在TS playground 中,如果我以 ES5 或更低版本为目标,则会出现错误...
  • certainly an error,但我无法重现"string" is not a string 的任何内容。我刚刚看到“字符串”不是数组”,这是真的。你能在可链接的 Web IDE 中重现错误吗?

标签: typescript spread-syntax


【解决方案1】:

我有 Angular-CLI (8) - 在我的情况下修改 tsconfig.json"target": "es2015"(或 es2018)根本没有帮助。只有添加 "downlevelIteration": true 有帮助。这是我的整个文件

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

【讨论】:

    【解决方案2】:

    扩展异端猴子的评论:

    将目标从 es5 更改为 es2015es6 可解决此问题。为了清楚起见,这是我完整的tsconfig.json

    {
      "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

    旁注:"downlevelIteration": true 也修复了它,但这对我来说似乎不是正确的解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-06
      • 1970-01-01
      • 2018-04-24
      • 2019-02-27
      • 2021-09-20
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      相关资源
      最近更新 更多