使用 TypeScript 和 Rollup 构建迭代器
Building An Iterator With TypeScript And Rollup
我正在用 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
class 上实现为 属性。这是尝试使用它的方法:
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);
}
}
}
我没有时间浪费在这上面,而且我在 SO 上发现的与此相关的所有其他问题都没有得到解答,这让我认为没有令人满意的解决方案。
我最终删除了 Radius
class 并将其添加到 Piece
class:
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);
}
}
}
我正在用 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
class 上实现为 属性。这是尝试使用它的方法:
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);
}
}
}
我没有时间浪费在这上面,而且我在 SO 上发现的与此相关的所有其他问题都没有得到解答,这让我认为没有令人满意的解决方案。
我最终删除了 Radius
class 并将其添加到 Piece
class:
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);
}
}
}