Gabe O'Leary
Gabe O'Leary

Reputation: 2070

Using fetch in Typescript project

I'm working on a typescript project that makes use of fetch at some point.

I know that I can use typings to make the definition of fetch globally available:

typings install --global --save dt~whatwg-fetch & typings install --global --save dt~whatwg-streams

BUT I am trying to move away from my dependency on typings and instead use npm and @types.

I can't figure out how to make this work however.

I've done npm install @types/whatwg-fetch and adding fetch to the compilerOptions.types array of my tsconfig.json file but I still get an error: Error Cannot find type definition file for 'fetch'.

Upvotes: 0

Views: 1686

Answers (1)

Gabe O'Leary
Gabe O'Leary

Reputation: 2070

I discovered what I was doing wrong:

incorrect tsconfig.json:

{
  "compilerOptions": {
    ...
    "types": ["fetch"]
  },
}

correct tsconfig.json:

{
  "compilerOptions": {
    ...
    "types": ["whatwg-streams","whatwg-fetch"]
  },
}

I mistakenly thought the types array was supposed to contain the type that was to be globally included, not the name of the typedef file which contains the types that are to be globally included.

Upvotes: 1

Related Questions