我正在使用TypeScript编写应用程序,我使用Rollup将文件捆绑在一起,然后使用Buble / Babel将已编译的Javascript转换为浏览器可以使用的内容。但是,当我运行rollup -c
时,出现错误:
semantic error TS2495 Type 'Radius' is not an array type or a string type.
不知道该如何处理,因为似乎没有关于TypeScript中迭代器的大量信息,我可以通过普通的搜索引擎路径找到它。这是我的文件:
rollup.config.ts
import typescript from 'rollup-plugin-typescript2';
import uglify from 'rollup-plugin-uglify';
import buble from 'rollup-plugin-buble';
export default {
input: "./chess-player.ts",
output: {
file: "./chess-player.min.js",
format: "iife"
},
watch: {
include: [
"./chess-player.ts",
"./src/*/*.ts",
"./src/*.ts"
]
},
plugins: [
typescript(),
buble(),
uglify()
]
};
tsconfig.json:
{
"compilerOptions": {
"target": "ES2015",
"module": "ES2015",
"strict": true,
"removeComments": true,
"esModuleInterop": true
}
}
radius.ts:
export class Radius implements IterableIterator<number> {
counter: number;
max?: number;
[Symbol.iterator](): IterableIterator<number> {
return this;
}
next(): IteratorResult<number> {
if (!this.max || this.counter < this.max) {
this.counter++;
return {
value: this.counter,
done: false
};
} else {
this.counter = 0;
return {
value: undefined,
done: true
};
}
}
constructor(max?: number) {
this.counter = 0;
this.max = max;
}
}
Radius
的实例作为Piece
类的属性实现。以下是尝试使用它的方法:
checkAttackRadius(boardElement: HTMLElement, pieceSquare: Square): void {
const piece = pieceSquare.piece;
let vectors = piece.attacks();
for (let radius of piece.radius) {
const remaining = this.remaining(vectors);
if (radius > 14 || remaining === 0) {
break;
}
for (let j = 0; j < vectors.length; j++) {
this.checkAttackVector(boardElement, pieceSquare, vectors[j], radius);
}
}
}
答案 0 :(得分:0)
我没有时间浪费在这上面,除了我发现的与SO相关的所有其他问题都没有答案,这让我觉得没有令人满意的解决方案。
我最终删除了Radius
类并将其添加到Piece
类:
radius(): { value: number, done: boolean } {
while(!this.max || this.counter < this.max) {
this.counter++;
return {
value: this.counter,
done: false
};
}
this.counter = 0;
return {
value: undefined,
done: true
};
}
它基本上是一个不使用任何ES2015特定接口的发生器。然后我就把它的用法改成了像这样的while循环:
checkAttackRadius(boardElement: HTMLElement, pieceSquare: Square): void {
const piece = pieceSquare.piece;
let vectors = piece.attacks();
let radius = piece.radius();
while (!radius.done && radius.value <= 14 && this.remaining(vectors) > 0) {
radius = piece.radius();
for (let j = 0; j < vectors.length; j++) {
this.checkAttackVector(boardElement, pieceSquare, vectors[j], radius.value);
}
}
}