当前位置: 动力学知识库 > 问答 > 编程问答 >

angular2 - 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'.

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: