使用TypeScript和汇总构建迭代器

亚伦

我正在用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);
        }
    }
}
亚伦

我没有时间浪费在这个问题上,除了我在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);
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章