【发布时间】:2023-02-14 17:12:14
【问题描述】:
我在使用 useSWRInfinite 和返回数组的 getKey 函数时遇到问题。以下是一个简化的示例,我在 fetcher 函数的参数上遇到了这个打字稿错误。
Type 'string | [any, ...unknown[]] | readonly [any, ...unknown[]] | Record<any, any>' is not an array type.
有没有办法缩小 getKey 函数的返回类型,以便我可以解构数组键?
import useSWRInfinite, { SWRInfiniteKeyLoader } from "swr/infinite";
const getKey: SWRInfiniteKeyLoader = (pageNum, page) => {
const lastItem = page.items[page.items.length - 1];
return ["Items", lastItem.id];
};
const resp = useSWRInfinite(
getKey, ([_, lastItemId]) => fetch('/items', {body: JSON.stringify({startingAfter: lastItemId})})
)
【问题讨论】:
标签: typescript swr