Shaun Luttin
Shaun Luttin

Reputation: 141742

Cannot find name 'Symbol' in whatwg-fetch type declaration file

The d.ts file at dt~whatwg-fetch includes the following declaration.

declare class Headers {
    // other code ommited
    [Symbol.iterator](): IterableIterator<[string, string]>;
}

We've tried to create a workaround interface without success. None of the following work. What will?

interface Symbol {}

interface Symbol<T> {}

interface Symbol {
   iterator: IterableIterator<[string, string]> 
}

interface Symbol<T> {
   iterator: IterableIterator<[string, string]> 
}

Edit

We've seen Typescript Cannot find name 'IterableIterator' and the recommendation is to target es6. Is that required?

http://www.typescriptlang.org/docs/handbook/iterators-and-generators.html

Upvotes: 12

Views: 5655

Answers (2)

Ricardo
Ricardo

Reputation: 101

Something that helped me to figure it out was adding the following setting to tsconfig.json:

"lib": [
    "es6",
    "dom"
]

After that i got another error of duplicate Promise declaration but i just removed the polyfill typing definition and worked fine. It looks like the whatwg-fetch doesn't really needs the ES6 Symbol, the code can use the ES5 fallback: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/11290

Upvotes: 9

Nitzan Tomer
Nitzan Tomer

Reputation: 164447

Symbol is new to ES6 and because of that you'll need to target your compiler to that.
To do that specify that the target in the compiler options is "es6".

The definition for Symbol can be found in the lib.es6.d.ts (while it's missing in the default lib.d.ts)


Edit

You can just polyfill that part yourself, you just need to copy the needed parts from the lib.es6.d.ts file and put it in your own file and reference it.

I copied what needed for the code you posted and it's in this playground.
It might have more than actually needed, so play around with that.

Upvotes: 16

Related Questions