Typescript: Fetch API definitions

Created on 24 Sep 2015  路  29Comments  路  Source: microsoft/TypeScript

Apologies if this is mentioned elsewhere, but I couldn't find an issue/PR that mentions it.

The Fetch API is still experimental, but I assume its definitions will make their way into TypeScript core at some point?

lib.d.ts help wanted

Most helpful comment

Be the PR you want to see in the world :)

All 29 comments

Reference the current DefinitelyTyped definitions.

Yes, when an API is standardized/well supported we'll make sure it's in lib.d.ts (or whatever appropriate sub component). We generally generate these libraries from spec's so we don't need issues per new API. In the meantime if you're hoping for improvements to the DefinitelyTyped definitions that's something we should track with an issue over there.

@danquirk - thanks, we're raising a PR to fix up one deviation from the spec.

Still not supported ?

Now that libs are split into multiple includable files, I think the fetch definition should be added in one of those lib files or in its own one.

PRs welcomed. You can find more information about contributing lib.d.ts fixes at https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md#contributing-libdts-fixes.

There are already typings for it on DefinitelyTyped. Hopefully those could be moved to lib, then we could deprecate the @types/whatwg-fetch package. It apparently depends on whatwg-streams, so it's likely best to do these both together.

All of Web IDL definitions are here:
https://src.chromium.org/viewvc/blink/trunk/Source/modules/fetch/

There is also this tool for converting Web IDL to TSD files: https://github.com/RicoP/idl2typings

Anyone wants to actually do it?

Be the PR you want to see in the world :)

The PR would just be to add the fetch typings to lib.d.ts right?

@QuantumInformation pr already at https://github.com/Microsoft/TypeScript/pull/12493 馃尮

@basarat roger that!

@mhegazy Which version of TS is this expected to land in? And is there anything special to do in tsconfig.json to enable the fetch definitions?

As noted in the milestone, [email protected]

Hi, im not pretty sure what im' writing in correct topic, but i guess i have related issue.
in project i use several libs which in dependencies have isomorphic-fetch & whatwg-fetch. after update to typescript@>=2.1.5 i recieve errors:

Error - typescript - node_modules\@types\isomorphic-fetch\index.d.ts(6,14): error TS2300: Duplicate identifier 'RequestContext'.
Error - typescript - node_modules\@types\isomorphic-fetch\index.d.ts(14,14): error TS2300: Duplicate identifier 'RequestMode'.
Error - typescript - node_modules\@types\isomorphic-fetch\index.d.ts(15,14): error TS2300: Duplicate identifier 'RequestCredentials'.
Error - typescript - node_modules\@types\isomorphic-fetch\index.d.ts(16,14): error TS2300: Duplicate identifier 'RequestCache'.
Error - typescript - node_modules\@types\isomorphic-fetch\index.d.ts(20,14): error TS2300: Duplicate identifier 'ResponseType'.
Error - typescript - node_modules\@types\isomorphic-fetch\index.d.ts(22,14): error TS2300: Duplicate identifier 'HeaderInit'.
Error - typescript - node_modules\@types\isomorphic-fetch\index.d.ts(23,14): error TS2300: Duplicate identifier 'BodyInit'.
Error - typescript - node_modules\@types\isomorphic-fetch\index.d.ts(24,14): error TS2300: Duplicate identifier 'RequestInfo'.
Error - typescript - node_modules\@types\isomorphic-fetch\index.d.ts(41,15): error TS2300: Duplicate identifier 'Headers'.
Error - typescript - node_modules\@types\isomorphic-fetch\index.d.ts(60,15): error TS2300: Duplicate identifier 'Body'.
Error - typescript - node_modules\@types\isomorphic-fetch\index.d.ts(81,15): error TS2300: Duplicate identifier 'Request'.
Error - typescript - node_modules\@types\isomorphic-fetch\index.d.ts(115,13): error TS2300: Duplicate identifier 'fetch'.
Error - typescript - node_modules\@types\whatwg-fetch\index.d.ts(6,15): error TS2300: Duplicate identifier 'Request'.
Error - typescript - node_modules\@types\whatwg-fetch\index.d.ts(29,6): error TS2300: Duplicate identifier 'RequestContext'.
Error - typescript - node_modules\@types\whatwg-fetch\index.d.ts(37,6): error TS2300: Duplicate identifier 'RequestMode'.
Error - typescript - node_modules\@types\whatwg-fetch\index.d.ts(39,6): error TS2300: Duplicate identifier 'RequestCredentials'.
Error - typescript - node_modules\@types\whatwg-fetch\index.d.ts(40,6): error TS2300: Duplicate identifier 'RequestCache'.
Error - typescript - node_modules\@types\whatwg-fetch\index.d.ts(48,15): error TS2300: Duplicate identifier 'Headers'.
Error - typescript - node_modules\@types\whatwg-fetch\index.d.ts(59,15): error TS2300: Duplicate identifier 'Body'.
Error - typescript - node_modules\@types\whatwg-fetch\index.d.ts(69,15): error TS2300: Duplicate identifier 'Response'.
Error - typescript - node_modules\@types\whatwg-fetch\index.d.ts(82,6): error TS2300: Duplicate identifier 'ResponseType'.
Error - typescript - node_modules\@types\whatwg-fetch\index.d.ts(90,14): error TS2300: Duplicate identifier 'HeaderInit'.
Error - typescript - node_modules\@types\whatwg-fetch\index.d.ts(91,14): error TS2300: Duplicate identifier 'BodyInit'.
Error - typescript - node_modules\@types\whatwg-fetch\index.d.ts(92,14): error TS2300: Duplicate identifier 'RequestInfo'.
Error - typescript - node_modules\@types\whatwg-fetch\index.d.ts(98,13): error TS2300: Duplicate identifier 'fetch'.

i can't remove one of such definitions because they are dependencies from third part packages and they are installed (@types/isomorphic-fetch, @types/whatwg-fetch) own td's.

ps. i used some packages from MS to create webparts with react and apollo-client to connect to graphql.

Please does anyone know how to resolve this conflict?

@types/isomorphic-fetch is not authored correctly, (i.e. it puts declarations in the global scope, hence the duplicate declaration errors) this we can fix.

@types/whatwg-fetch has to be removed if you are using TS 2.2. Seems that the other @types package that depend on it is @type/service_worker_api and that would not be needed any longer.

If there are other dependencies, then you need them to publish a new version before using TS 2.2

All this errors were appeared after install npm install apollo-client react-apollo graphql-tag --save

All this errors were appeared after install npm install apollo-client react-apollo graphql-tag --save

we need to fix isomorphic-fetch first, then ask apollo-client to update their package.

@mhegazy I don't see an mention of lib.d.ts being updated in the Breaking Changes doc. Can you add it and explain what you wrote above (existing projects using fetch.d.ts, etc will need to remove it).

@mhegazy I don't see an mention of lib.d.ts being updated in documented in the Breaking Changes doc. Can you add it and explain what you wrote above (existing projects using fetch.d.ts, etc will need to remove it).

Yes. i have not fully updated the docs for the release yet. hopefully will do this this week. I still need to add the breaking changes, finish the what's new, and clean up the roadmap :)

Documentation added to breaking changes page: https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#changes-to-dom-apis-in-the-standard-library

@mhegazy - Sorry, just to be clear -- if I'm working on a nodejs app, do I still need any kind of fetch polyfill, or does TypeScript provide it regardless of environment?

What are the correct steps in every scenario and do I still need any third party libraries, or is TypeScript fixing fetch "once and for all"??

@mhegazy - Sorry, just to be clear -- if I'm working on a nodejs app, do I still need any kind of fetch polyfill, or does TypeScript provide it regardless of environment?

The fetch API is included as part of the DOM API, so it is part of lib.dom.d.ts.

If you are using node, you are not interested in the DOM, so my recommendation is to use --lib es6 to get the core ES6 lib without the DOM. in this case you would not get the Fetch API. The correct thing to do here is to add that to @types/node

@atrauzzi These are typings only, there is no polyfill included for fetch just like there is no polyfill included for Promise.

There are very few polyfills included with typescript: class extends and async/await are really the only two I can think of. I suspect we won't see many more polyfills included in the future.

So when are we expecting @types/isomorphic-fetch to be patched to work with the new typings?

See DefinitelyTyped/DefinitelyTyped#14796

@types/[email protected] should have the fix.

It would be nice if the updated typings have kept the parity with the whatwg-fetch types.

Specifically the typed version of Body#json<T>() would be nice to have: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/whatwg-fetch/index.d.ts#L39

Was this page helpful?
0 / 5 - 0 ratings

Related issues

siddjain picture siddjain  路  3Comments

manekinekko picture manekinekko  路  3Comments

jbondc picture jbondc  路  3Comments

DanielRosenwasser picture DanielRosenwasser  路  3Comments

uber5001 picture uber5001  路  3Comments