在 Typescript 项目中使用 fetch
Using fetch in Typescript project
我正在开发一个有时会使用 fetch 的 typescript 项目。
我知道我可以使用类型来使 fetch 的定义全局可用:
typings install --global --save dt~whatwg-fetch
&
typings install --global --save dt~whatwg-streams
但是我正在尝试摆脱对类型的依赖,而是使用 npm 和 @types。
不过,我不知道该怎么做。
我已完成 npm install @types/whatwg-fetch
并将提取添加到我的 tsconfig.json 文件的 compilerOptions.types 数组,但我仍然收到错误消息:Error Cannot find type definition file for 'fetch'.
我发现我做错了什么:
不正确tsconfig.json:
{
"compilerOptions": {
...
"types": ["fetch"]
},
}
正确tsconfig.json:
{
"compilerOptions": {
...
"types": ["whatwg-streams","whatwg-fetch"]
},
}
我错误地认为 types 数组应该包含要全局包含的类型,而不是包含要全局包含的类型的 typedef 文件的名称。
我正在开发一个有时会使用 fetch 的 typescript 项目。
我知道我可以使用类型来使 fetch 的定义全局可用:
typings install --global --save dt~whatwg-fetch
&
typings install --global --save dt~whatwg-streams
但是我正在尝试摆脱对类型的依赖,而是使用 npm 和 @types。
不过,我不知道该怎么做。
我已完成 npm install @types/whatwg-fetch
并将提取添加到我的 tsconfig.json 文件的 compilerOptions.types 数组,但我仍然收到错误消息:Error Cannot find type definition file for 'fetch'.
我发现我做错了什么:
不正确tsconfig.json:
{
"compilerOptions": {
...
"types": ["fetch"]
},
}
正确tsconfig.json:
{
"compilerOptions": {
...
"types": ["whatwg-streams","whatwg-fetch"]
},
}
我错误地认为 types 数组应该包含要全局包含的类型,而不是包含要全局包含的类型的 typedef 文件的名称。