Typescript 获取';调用createHttpLink时s类型不匹配
按照此处的说明进行操作: 要执行服务器端渲染,我遇到以下错误:Typescript 获取';调用createHttpLink时s类型不匹配,typescript,apollo,server-side-rendering,Typescript,Apollo,Server Side Rendering,按照此处的说明进行操作: 要执行服务器端渲染,我遇到以下错误: Invariant Violation: fetch is not found globally and no fetcher passed, to fix pass a fetch for your environment like https://www.npmjs.com/package/node-fetch. For example: import fetch from 'node-fetch'; import { cr
Invariant Violation:
fetch is not found globally and no fetcher passed, to fix pass a fetch for
your environment like https://www.npmjs.com/package/node-fetch.
For example:
import fetch from 'node-fetch';
import { createHttpLink } from 'apollo-link-http';
const link = createHttpLink({ uri: '/graphql', fetch: fetch });
我添加了推荐的代码,导入获取
,将其传递到createHttpLink
,我还安装了@types/node fetch
,但我收到了以下警告/错误:
Error:(75, 7) TS2322: Type 'typeof fetch' is not assignable to type '(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>'.
Types of parameters 'url' and 'input' are incompatible.
Type 'RequestInfo' is not assignable to type 'import("C:/Users/pupeno/Documents/Flexpoint Tech/js/exp5/node_modules/@types/node-fetch/index").RequestInfo'.
Type 'Request' is not assignable to type 'RequestInfo'.
Type 'Request' is missing the following properties from type 'Request': context, compress, counter, follow, and 6 more.
而fetch
的预期类型为:
declare function fetch(
url: RequestInfo,
init?: RequestInit
): Promise<Response>;
fetch?: WindowOrWorkerGlobalScope['fetch'];
我不确定如何确切地找到WindowOrWorkerGlobalScope
,我的IDE指向了两个可能的定义,一个在dom
中:
fetch(input: RequestInfo, init?: RequestInit): Promise<Response>;
他们看起来一模一样
我可以继续下去的兔子洞的类型,但这是否敲响了一个钟?我是否应该使用不同的
获取
?节点获取
仅在node.js上实现完整的获取API
但是,createHttpLink
不太可能使用这些未实现的部分。因此,在此处禁用类型检查应该是安全的:
const-link=createHttpLink({uri:'/graphql',fetch:fetch as any});
可能值得向阿波罗项目提交一个问题,要求他们将预期类型限制为实际使用的类型。@AluanHaddad来自
github/fetch
自述:“这个项目在Node.js环境下不工作。”因此我认为这不是一个可行的解决方案。谢谢
fetch(input: RequestInfo, init?: RequestInit): Promise<Response>;