17

I have problem with my code in typescript After compile by tsc, I have errors like about cannot find some names.

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'.

My code is:

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());

version tsc is Version 2.1.0-dev.20160716

5
  • 2
    What's your target in the compilation options? It should be ES6 Commented Jul 17, 2016 at 11:54
  • My target is ES6: { "compilerOptions": { "target": "ES6", "module": "commonjs", "sourceMap": true } } Commented Jul 17, 2016 at 12:19
  • 2
    I'm able to compile your code with target ES6 (using tsc version 1.8.10). You might have other things that affect the compilation (check this). Commented Jul 17, 2016 at 12:34
  • 2
    Problem was that tsc cannot see my tsconfig.json, after run tsc -t ES6 app.ts code was compiled properly. Commented Jul 17, 2016 at 12:38
  • 3
    Consider writing your last comment as a self-answer; after some time you will also be able to accept it, so that future users with a similar problem would benefit from it. Commented Jul 17, 2016 at 13:27

2 Answers 2

14

Problem was that tsc cannot see my tsconfig.json, after run tsc -t ES6 app.ts code was compiled properly.

Sign up to request clarification or add additional context in comments.

1 Comment

This only works as long as you want to compile a ES6 TypeScript file to a ES6 JavaScript file, what does not make any sense for me. So you could just change the file suffix .ts to .js. I am sceptical, if that is the correct answer.
12

I solve the same problem with tsc -v 3.5.3 and @types/node (tested es5 and es6)

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

and add "node" in the tsconfig.json

"compilerOptions": {
  "types": [
    "./",
    "node"
  ]
}

Take a look here DefinitelyTyped for complete node types.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.