Type 'IterableIterator<number>' 不是数组类型也不是字符串类型
Type 'IterableIterator<number>' is not an array type or a string type
我在尝试生成动态数字范围时遇到以下错误。
Type 'IterableIterator<number>' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.
我发现了一个现有问题,但解决方案对我不起作用。
我的设置如下...
- 我使用了命令
npx create-next-app@latest --ts
class FlexGrid extends React.Component {
range(size: number, startAt = 0) {
return [...Array(size).keys()].map(i => i + startAt);
}
我试过设置target: es6
和downlevelIteration: true
都没有解决错误。
{
"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"]
}
显然它是严格的,不想使用迭代器(不是数组)的传播来创建数组。但是,可以使用 Array.from
函数从迭代器创建数组:
range(size: number, startAt = 0) {
return Array.from(Array(size).keys()).map((i) => i + startAt);
}
Array.from
接受 Iterable
.
我在尝试生成动态数字范围时遇到以下错误。
Type 'IterableIterator<number>' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of iterators.
我发现了一个现有问题,但解决方案对我不起作用。
我的设置如下...
- 我使用了命令
npx create-next-app@latest --ts
class FlexGrid extends React.Component {
range(size: number, startAt = 0) {
return [...Array(size).keys()].map(i => i + startAt);
}
我试过设置target: es6
和downlevelIteration: true
都没有解决错误。
{
"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"]
}
显然它是严格的,不想使用迭代器(不是数组)的传播来创建数组。但是,可以使用 Array.from
函数从迭代器创建数组:
range(size: number, startAt = 0) {
return Array.from(Array(size).keys()).map((i) => i + startAt);
}
Array.from
接受 Iterable
.