How to enforce usequery as NonNullable

Mmugeenn3/21/2023
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
    },
  })
Nnlucas3/21/2023
The type of what is undefined?
Mmugeenn3/21/2023
the type of data will be in this case always the TYPE | undefined.
const { data } = useAppContext()
Mmugeenn3/21/2023
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
Nnlucas3/21/2023
Ah right, this is a react-query question, and I think the answer is it's not possible
Nnlucas3/21/2023
react-query just defines that value as possibly undefined, because in theory at any point it could be and typescript is static
Nnlucas3/21/2023
I guess that might change when suspense becomes a bigger thing
Mmugeenn3/21/2023
Alright, I will ask tho at react-query but I see your point.