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
{{ message }}
Repository navigation
Type narrowing for data after checking error
#3741
I'm currently using urql and encountered a situation where I couldn't narrow down the type of data after checking error. Here's an example:
import{registerUrql}from'@urql/next/rsc';constmakeClient=()=>{
...
};const{ getClient }=registerUrql(makeClient);exportdefaultasyncfunctionHome(){const{ data, error }=awaitgetClient().query(PokemonsQuery,{});if(!error)returnnull;console.log(data);// type is object | undefined return ...
}
Even though I've checked the error and ensured the code won't run when error is undefined, the type of data remains object | undefined.
Would it be possible to narrow the type of data to just object in this case?
exportdefaultasyncfunctionHome(){const{ data, error }=awaitgetClient().query(PokemonsQuery,{});if(!error)returnnull;console.log(data);// type is objectreturn ...
}
I may not fully understand the behavior of this library, so I would appreciate any clarification or guidance on whether this is possible.
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
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I'm currently using urql and encountered a situation where I couldn't narrow down the type of data after checking error. Here's an example:
Even though I've checked the error and ensured the code won't run when
errorisundefined, the type ofdataremainsobject | undefined.Would it be possible to narrow the type of
datato justobjectin this case?I may not fully understand the behavior of this library, so I would appreciate any clarification or guidance on whether this is possible.
Thank you for your help!
All reactions