You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Right now, the arguments for the fetcher function are untyped.
useSWR(()=>`users/${id}`,(uid: string)=>{// uid is an untyped string})
It would be nice if the type from the key got typed onto the first argument of the fetcher function. Since the key can be null, it could be the NonNullable type of the key. And if it's a function, it could be ReturnType<NonNullable<Key>>
Some pseudo code:
typeKeyOrGetter<Key>=Keyextends(...args: any)=> infer ReturnValue ? NonNullable<ReturnValue> : NonNullable<Key>typeFetcher<Key,Data>=(key: KeyOrGetter<Key>)=>Data|Promise<Data>typeKeyPrimitive=any[]|null|string|readonlyany[]functionuseSWR<Keyextends(()=>KeyPrimitive)|KeyPrimitive,Data>(key: Key,fetcher: Fetcher<Key,Data>){}useSWR('hi',(key)=>{typeofkey// string})useSWR(()=>'hey',(key)=>{typeofkey// string})useSWR(()=>{if(!true){thrownewError()}return['users']asconst},key=>{hoveroverkey// ["users"]// notice that this accounts for string literals!})
I added readonly any[] as one of the key options so that you can pass string literals as the key results.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Right now, the arguments for the fetcher function are untyped.
It would be nice if the type from the key got typed onto the first argument of the
fetcher
function. Since the key can be null, it could be theNonNullable
type of the key. And if it's a function, it could beReturnType<NonNullable<Key>>
Some pseudo code:
I added
readonly any[]
as one of the key options so that you can pass string literals as the key results.Example of the code here.
Notice that when you hover over the
key
in the fetcher function, it is typed based on the first argument.Beta Was this translation helpful? Give feedback.
All reactions