Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[whatwg-fetch] requires ES6 target or node.d.ts but doesnt need to #11290

Closed
danmortimer opened this issue Sep 16, 2016 · 6 comments
Closed

[whatwg-fetch] requires ES6 target or node.d.ts but doesnt need to #11290

danmortimer opened this issue Sep 16, 2016 · 6 comments

Comments

@danmortimer
Copy link

the whatwg-fetch typings reference the interfaces Symbol and IterableIterator so if you target ES5 it fails to build. The fetch project it matches at https://github.com/github/fetch has built in fallbacks for the iterators and is meant to be used with ES5. So it shouldnt reference those ES6 types.
I cant use the typings as they are without including core-js or node.d.ts or targeting ES6, which is not actually required by the library itself.

@vvakame
Copy link
Member

vvakame commented Sep 19, 2016

please send a pull request. I'll review it.

@tobico
Copy link
Contributor

tobico commented Oct 1, 2016

Looks like the problem lines are these ones, defining an iterator interface for the Headers class - https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/whatwg-fetch/whatwg-fetch.d.ts#L24-L28

These methods do exist, but they're not essential to use the library, and the type they rely on doesn't exist without ES6.

Anyone know of a way to make these definitions conditional on the types actually existing? Or any other nice way to handle this without breaking things for either use case?

@vvakame
Copy link
Member

vvakame commented Oct 3, 2016

// compat for TypeScript 1.8
// if you use with --target es3 or --target es5 and use below definitions,
// use the lib.es6.d.ts that is bundled with TypeScript 1.8.
interface MapConstructor { }
interface WeakMapConstructor { }
interface SetConstructor { }
interface WeakSetConstructor { }

We can use this pattern.

@seansfkelley
Copy link
Contributor

The workaround I used for this was to install the typings for es6-promise and then add these to a miscellaneous/catch-all "extra typings" file I have around:

type IterableIterator<T> = any;
type IteratorResult<T> = any;
declare const Symbol: {
  iterator: symbol;
};

I wasn't able to find all of these typings shimmed out elsewhere, so I just basically exempted the whole from type checking.

@danmortimer
Copy link
Author

danmortimer commented Oct 6, 2016

tobico:

These methods do exist, but they're not essential to use the library, and the type they rely on doesn't exist without ES6.

That actually isn't really the case.
Check this: https://github.com/github/fetch/blob/master/fetch.js#L40-L56

If it sees that Symbol.iterator is supported (either es6 or a polyfill) then it uses those types. Otherwise it shims in the basic iterator functionality.

Because of this, I think the best option is to type the iterator shim.
That could be combined with the pattern suggested by vvakame, and set those methods to return union types so it can use either the ES6 type or the shim.

@orta
Copy link
Collaborator

orta commented Jun 7, 2021

Hi thread, we're moving DefinitelyTyped to use GitHub Discussions for conversations the @types modules in DefinitelyTyped.

To help with the transition, we're closing all issues which haven't had activity in the last 6 months, which includes this issue. If you think closing this issue is a mistake, please pop into the TypeScript Community Discord and mention the issue in the definitely-typed channel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants