[英]How can specify type for …rest in typescript-react?
const { data, error, ...rest } = useSWR(
id ? `/api/v1/blogs/${id}` : null,
fetcher
);
如何指定...rest
的類型?
const {data,error,...rest} : {data:any,error:Error,"I dont know what to place for ...rest"}
如果useSWR
的返回值已經是已知類型,則不需要,TypeScript 將知道...rest
中的內容。 如果沒有,您可以指定其他元素的類型。
interface Foo {
a: number
b: string
c: boolean
}
const { a, ...rest }: Foo = { a: 1, b: '2', c: false }
如果這對您不起作用(如果您沒有用於 useSWR 的類型並且不想寫出返回對象中的其他字段),您可以將其稱為 Object 或{}
,然后轉換data
和error
然后。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.