xenoterracide xenoterracide - 1 year ago 286
TypeScript Question

How do I import Request/Response when using the fetch API?

I see the following in my

so I'm going to assume that it provides what I need.

"@types/whatwg-fetch": "0.0.27",

The classes I need seem to be
. I tried this, but Intellij says it's wrong, and so does the webpack server

import { Request, Response } from 'whatwg-fetch';

the error from the webpack dev server

File '/home/xenoterracide/IdeaProjects/rpf-ui/node_modules/@types/whatwg-fetch/index.d.ts' is not a module.

and yet

stat /home/xenoterracide/IdeaProjects/rpf-ui/node_modules/@types/whatwg-fetch/index.d.ts
File: '/home/xenoterracide/IdeaProjects/rpf-ui/node_modules/@types/whatwg-fetch/index.d.ts'
Size: 2662 Blocks: 8 IO Block: 4096 regular file
Device: fe00h/65024d Inode: 7750050 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 1000/xenoterracide) Gid: ( 1000/xenoterracide)
Access: 2016-08-08 19:18:56.205552681 -0500
Modify: 2016-07-14 08:52:54.000000000 -0500
Change: 2016-08-08 19:17:19.288571256 -0500
Birth: -

How can I import these classes?

Answer Source

whatwg-fetch is a polyfill for the fetch API which includes the Request and Response interfaces. There's no need to import Request/Response because they are part of the global namespace.


The typescript definitions file for whatwg-fetch uses declare class Request { ... } to add the class to the global namespace. It doesn't export the class as part of a module which is why you get the ...____... is not a module error.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download