Mugetsu
Mugetsu17mo ago

How to enforce usequery as NonNullable

I have a custom hook with infinite query. I check for undefined at the app first render and then it is reused so I know by then, the type should not be undefined. I would like to be able to infer its type without undefined conditionally. Can I do that? How to if so??
export const useAppContext = (enabled = true) =>
api.appContext.getAppContext.useQuery(undefined, {
staleTime: Infinity,
enabled,
select: (data) => {
currentRoles = data?.authorisation?.roles ?? []
return data
},
})
export const useAppContext = (enabled = true) =>
api.appContext.getAppContext.useQuery(undefined, {
staleTime: Infinity,
enabled,
select: (data) => {
currentRoles = data?.authorisation?.roles ?? []
return data
},
})
4 Replies
Nick
Nick17mo ago
The type of what is undefined?
Mugetsu
Mugetsu17mo ago
the type of data will be in this case always the TYPE | undefined. const { data } = useAppContext() I would like it to be: 1st time (app top level) of type -> TYPE | undefined and then force it to be just -> TYPE so I dont have to do checking for undefined or mark chaining of data property with ! as I know at this time it's already in the cache and exists
Nick
Nick17mo ago
Ah right, this is a react-query question, and I think the answer is it's not possible react-query just defines that value as possibly undefined, because in theory at any point it could be and typescript is static I guess that might change when suspense becomes a bigger thing
Mugetsu
Mugetsu17mo ago
Alright, I will ask tho at react-query but I see your point.