Skip to content

Commit f795a67

Browse files
KyleAMathewsclaude
andauthored
Add helper to extract the result type from a query QueryResult (#1096)
* feat(db): Export ExtractContext type for extracting query result types This allows users to extract the result type from a Query instance: ```typescript import { Query, ExtractContext, GetResult } from '@tanstack/db' const myQuery = new Query() .from({ users }) .select(({ users }) => ({ name: users.name })) type MyQueryResult = GetResult<ExtractContext<typeof myQuery>> ``` Fixes a gap in the public API where ExtractContext was defined but not re-exported from the main query module. * chore: Add changeset for ExtractContext export * feat(db): Add QueryResult helper type for extracting query result types Adds a simpler API similar to Zod's z.infer: ```typescript import { Query, QueryResult } from '@tanstack/db' const myQuery = new Query() .from({ users }) .select(({ users }) => ({ name: users.name })) type MyQueryResult = QueryResult<typeof myQuery> ``` This is more ergonomic than the two-step approach: `GetResult<ExtractContext<typeof myQuery>>` --------- Co-authored-by: Claude <[email protected]>
1 parent b1cc4a7 commit f795a67

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
'@tanstack/db': patch
3+
---
4+
5+
Export `QueryResult` helper type for easily extracting query result types (similar to Zod's `z.infer`).
6+
7+
```typescript
8+
import { Query, QueryResult } from '@tanstack/db'
9+
10+
const myQuery = new Query()
11+
.from({ users })
12+
.select(({ users }) => ({ name: users.name }))
13+
14+
// Extract the result type - clean and simple!
15+
type MyQueryResult = QueryResult<typeof myQuery>
16+
```
17+
18+
Also exports `ExtractContext` for advanced use cases where you need the full context type.

packages/db/src/query/builder/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import type {
3030
import type {
3131
CompareOptions,
3232
Context,
33+
GetResult,
3334
GroupByCallback,
3435
JoinOnCallback,
3536
MergeContextForJoinCallback,
@@ -887,6 +888,9 @@ export type ExtractContext<T> =
887888
? TContext
888889
: never
889890

891+
// Helper type to extract the result type from a QueryBuilder (similar to Zod's z.infer)
892+
export type QueryResult<T> = GetResult<ExtractContext<T>>
893+
890894
// Export the types from types.ts for convenience
891895
export type {
892896
Context,

packages/db/src/query/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export {
1010
type Source,
1111
type GetResult,
1212
type InferResultType,
13+
type ExtractContext,
14+
type QueryResult,
1315
} from './builder/index.js'
1416

1517
// Expression functions exports

0 commit comments

Comments
 (0)