Javascript 向SWR发出请求时typescript出现问题
我想知道下面传递给我的函数的参数的类型是什么Javascript 向SWR发出请求时typescript出现问题,javascript,reactjs,typescript,next.js,swr,Javascript,Reactjs,Typescript,Next.js,Swr,我想知道下面传递给我的函数的参数的类型是什么 const fetcher = async (...args) => { ~_ 0 const res = await fetch(...args); 1
const fetcher = async (...args) => {
~_ 0 const res = await fetch(...args);
1
~ 2 return res.json();
3 };
这是我的SWR获取函数,这是我得到的错误
[tsserver 2556] [E] Expected 1-2 arguments, but got 0 or more.
SWR吊钩
const { error, data } = useSWR(`/api/albums/list/${user.id}`, fetcher)
这是
fetch
函数的TypeScript签名:
declare function fetch(input: RequestInfo, init?: RequestInit): Promise<Response>;
或者,许多参数(如四个参数):
然后,您使用调用。spread的参数不满足fetch的函数签名(参数不能为零或超过两个),因此tsc报告错误
所以你最好这样修改它:
const fetcher = async (
input: RequestInfo,
init: RequestInit,
...args: any[]
) => {
const res = await fetch(input, init);
return res.json();
};
软件包版本:
“typescript”:“^4.1.3”
既然您没有使用前两个参数之外的其他参数来执行任何操作,那么完全不使用…args
不是更好吗?@SimonW是的,我不确定获取程序是否需要其他参数。所以我保留了…args
fetcher("localhost", {}, {}, {});
const fetcher = async (
input: RequestInfo,
init: RequestInit,
...args: any[]
) => {
const res = await fetch(input, init);
return res.json();
};