TanStackT
TanStack2y ago
3 replies
faint-white

StrictNullCheck and dependant queries

Hi, me again !
I am working with typescript strict mode, and I am not sure what would be an elegant solution to handle dependant queries.

I have typesafe abstracted queries that require data to be set:
- useGetDetails
- useGetBillingInfoById requires pathParams.location_group_id to be a string

Here is an example:
export const App = () => {
  const detailsQuery = billingService.account.useGetDetails()
  const infoQuery = billingService.locationGroup.useGetBillingInfoById(
    {
      pathParams: {
        location_group_id: detailsQuery.data?.default_location_group_id // Type string | null | undefined  is not assignable to type string.
      }
    },
    { enabled: Boolean(detailsQuery.data?.default_location_group_id) }
  )
}


(1) Making weird condition that satisfy typesafety
export const App = () => {
  const detailsQuery = billingService.account.useGetDetails()
  const infoQuery = billingService.locationGroup.useGetBillingInfoById(
    {
      pathParams: {
        location_group_id: detailsQuery.data?.default_location_group_id || '' // HERE
      }
    },
    { enabled: Boolean(detailsQuery.data?.default_location_group_id) }
  )
}


(2) Have optional types on useGetBillingInfoById for the property pathParams.location_group_id

(3) Define initialData, but usually I have to define the whole data
export const App = () => {
  const detailsQuery = billingService.account.useGetDetails({ 
    initialData: {default_location_group_id: ''} // Error as the Type is not complete
  })
  const infoQuery = billingService.locationGroup.useGetBillingInfoById(
    {
      pathParams: {
        location_group_id: detailsQuery.data?.default_location_group_id
      }
    },
    { enabled: Boolean(detailsQuery.data?.default_location_group_id) }
  )
}


(4) ?


Thank a lot ! :))
Was this page helpful?