TanStackT
TanStack7d ago
3 replies
dramatic-maroon

How to extract the return type from a Query instance?

I'm trying to extract the result type from a Query instance for use with generics and type inference.

The documentation mentions GetResult: https://tanstack.com/db/latest/docs/reference/type-aliases/GetResult

However, GetResult expects a Context type parameter, not a Query:
type Result = GetResult<typeof myQuery>

Example use case

export const myQuery = new Query()
  .from({ collection1 })
  .join(// Any join...)
  .select(({ ... }) => {
    const transformed = // any data transformation...
    return transformed
  })

// How do I extract this type?
export type MyQueryResult = ???


ExtractContext exists in the library but isn't exported, which would solve this:
type MyContext = ExtractContext<typeof myQuery>
type Result = GetResult<MyContext>


Questions:
1. Should ExtractContext be exported to enable this pattern?
2. Or is there a recommended alternative approach for extracting query result types?

Thanks
Was this page helpful?