javascript - TypeScript 错误 - 应有 1-2 个参数,但有 0 个或更多。TS2556
问题描述
我在 JavaScript 中使用了这个语句,但是当我尝试在 TypeScript 项目中使用它时出现错误。它在抱怨 fetch(...args)
const fetcher = (...args) => fetch(...args).then(response => response.json());
Expected 1-2 arguments, but got 0 or more. TS2556
解决方案
这应该可以帮助您:
const fetcher = (...args: [input: RequestInfo, init?: RequestInit | undefined]) => fetch(...args).then(response => response.json());
您应该为fetch
. 它需要 1-2 个参数。
如果你想使用rest
运算符,你应该告诉 TS,你也期望在你的高阶函数中有两个参数
更新
通用方法。如果要获取任何函数的参数类型,只需使用Parameters util。
type FetchParameters = Parameters<typeof fetch>
推荐阅读
- python - 将(IDW)空间数据插入栅格的最佳方法是什么?[R]
- javascript - 我无法截取我的背景 iframe
- powerbi - 显示哪些行在两个表之间改变了状态
- vb.net - VB.NET - 来自 KML 文件的多边形中的点
- mysql - LEFT OUTER JOIN 检查不存在的关系 id
- asp.net-core-mvc - 如何在 ASP.NET Core MVC 中绑定 Part 视图和局部视图中的所有子模型
- c# - Android(Xamarin):BottomNavigationView 没有出现在正确的位置
- python - 我做了 FFT(快速傅里叶变换),但我无法理解......(python)
- svelte - 苗条的过渡:如何淡入,然后淡出?
- web-services - 有关如何从 Google Cloud Platform 调用外部网络服务的任何信息?