作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
TS 抛出奇怪的错误:
Error:(125, 18) TS2569: Type 'string' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.
> s = 'abcdef';
> r = [...s];
< (6) ["a", "b", "c", "d", "e", "f"]
const s: string = 'abcdef';
const res = [...s]; // <= Error: Type 'string' is not an array type or a string type
console.log(res);
"dependencies": {
"typescript": "^3.5.3"
}
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"
]
}
最佳答案
扩展异端猴子的评论:
从 es5
更改目标至 es2015
或 es6
解决了这个问题。这是我的全部 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"
]
}
"downlevelIteration": true
也修复了它,但这对我来说似乎不是正确的解决方案。
关于typescript - TS 错误 : Type 'string' is not an array type or a string type. 字符串如何不是字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56990364/
我是一名优秀的程序员,十分优秀!