Typescript找不到名字'IterableIterator'

Mar*_*iak 10 javascript typescript

我的打字稿中的代码有问题经过tsc编译后,我有类似的错误找不到一些名字.

app.ts(1,22): error TS2304: Cannot find name 'IterableIterator'.
app.ts(8,20): error TS2304: Cannot find name 'IteratorResult'.
app.ts(26,6): error TS2304: Cannot find name 'Symbol'.
app.ts(26,26): error TS2304: Cannot find name 'IterableIterator'.
Run Code Online (Sandbox Code Playgroud)

我的代码是:

class Fib implements IterableIterator<number> {
    protected fn1 = 0;
    protected fn2 = 1;

    constructor(protected maxValue?: number) {}

    public next(): IteratorResult<number> {
        var current = this.fn1;
        this.fn1 = this.fn2;
        this.fn2 = current + this.fn1;

        if (this.maxValue && current <= this.maxValue) {
            return {
                done: false,
                value: current
            }
        }

        return {
            done: true
        }
    }

    [Symbol.iterator](): IterableIterator<number> {
        eturn this;
    }
}

fib = new Fib();
console.log(fib.next());
Run Code Online (Sandbox Code Playgroud)

version tsc是版本2.1.0-dev.20160716

Mar*_*iak 9

问题是tsc无法看到我的tsconfig.json,运行tsc -t后,ES6 app.ts代码编译正确.


Shi*_*Sao 7

我用tsc -v 3.5.3and解决了同样的问题@types/node (测试了 es5 和 es6)

npm install @types/node --save--dev

并添加"node"tsconfig.json

"compilerOptions": {
  "types": [
    "./",
    "node"
  ]
}
Run Code Online (Sandbox Code Playgroud)

在此处查看绝对类型的完整节点类型。