提问人:FLC 提问时间:11/15/2023 更新时间:11/15/2023 访问量:26
应用被盗的提取类型来重新定义 urql 客户端提取成员不满足 Typescript 要求
Applying stolen fetch types to redefine urql Client fetch member doesn't satisfy Typescript
问:
在 @urql/core 上重新定义 fetch 函数时,我很难尝试满足 Typescript。
我找到了两个 SO 答案,可以解决问题,但不知何故它们对我不起作用:
我的代码如下所示:
import { Client } from "@urql/core"
const client = new Client({
fetch: async (...args: Parameters<typeof fetch>) => {
const response = await fetch(...args);
// ...
打字稿的抱怨是:
错误|类型 '(input: RequestInfo, init?: RequestInit | undefined) => Promise' 不可分配给类型 '{ (input: RequestInfo |URL, init?: RequestInit |undefined):承诺;(输入: RequestInfo, init?: RequestInit | undefined): Promise<...>;}'.参数“input”和“input”的类型不兼容。键入“RequestInfo |URL“不能分配给类型”RequestInfo”。类型“Request”不能分配给类型“RequestInfo”。属性“duplex”在类型“Request”中缺失,但在类型“import(”./node_modules/undici-types/fetch“)中是必需的。请求”。
(路径略有编辑以隐藏个人信息)
我尝试了一个简化的获取替换,只是为了寻求如何满足 Typescript,并发现这段代码工作正常:
const fetcher = async (...args: Parameters<typeof fetch>) => {
const response = await fetch(...args)
// ...
return response
}
虽然这个给了我与上面的初始片段完全相同的错误:
const originalFetch = global.fetch
global.fetch = async (...args: Parameters<typeof global.fetch>) => {
const response = await originalFetch(...args)
// ...
return response
}
我的主要问题是如何解决这个问题并满足 Typescript。
在伸手去之前,我尝试了不同的东西,每次尝试都给了我一个不同的 Typescript 错误:Parameters<>
// The initial wasn't typed
global.fetch = async (...args) => {
const response = await originalFetch(...args)
// No overload matches this call
// Secondly used the very same parameters shown on previous attempt error message
global.fetch = async (...args: [RequestInfo | URL, RequestInit | undefined]) => {
const response = await originalFetch(...args)
// but then it complains about missing property 'referrer' from the `undici` type
// Thirdly I removed the `URL` option from the first parameter
global.fetch = async (...args: [RequestInfo, RequestInit | undefined]) => {
const response = await originalFetch(...args)
// but it seems is needed cause "Type 'RequestInfo | URL' is not assignable to type 'RequestInfo'"
当我分配给任何名称时,看到它工作正常,但在尝试覆盖全局函数时中断,这真的很令人费解。由于我试图篡改来自 的响应数据,因此我无法使用其他方法,因为我必须替换 中的成员。@urql/core
fetch
Client
答: 暂无答案
评论