Skip to content

Commit fbed864

Browse files
committed
Fix: Reorder overloads to fix type inference
The poison pill overloads were matching BEFORE the specific overloads, causing TypeScript to infer DisabledQueryError for valid queries. TypeScript checks overloads top-to-bottom and uses the first match. Since QueryBuilder is assignable to QueryBuilder | undefined | null, the poison pill overloads were matching first. Solution: Move poison pill overloads to the END, just before the implementation. This ensures: 1. Specific overloads (without undefined) match first 2. Poison pill overloads (with undefined) only match when needed All tests now pass with no type errors.
1 parent d8c1731 commit fbed864

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

packages/react-db/src/useLiveSuspenseQuery.ts

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -113,32 +113,6 @@ type DisabledQueryError = {
113113
* )
114114
* ```
115115
*/
116-
// "Poison pill" overloads - catch disabled queries and show custom compile-time error
117-
export function useLiveSuspenseQuery<TContext extends Context>(
118-
queryFn: (
119-
q: InitialQueryBuilder
120-
) => QueryBuilder<TContext> | undefined | null,
121-
deps?: Array<unknown>
122-
): DisabledQueryError
123-
124-
export function useLiveSuspenseQuery<TContext extends Context>(
125-
queryFn: (
126-
q: InitialQueryBuilder
127-
) => LiveQueryCollectionConfig<TContext> | undefined | null,
128-
deps?: Array<unknown>
129-
): DisabledQueryError
130-
131-
export function useLiveSuspenseQuery<
132-
TResult extends object,
133-
TKey extends string | number,
134-
TUtils extends Record<string, any>,
135-
>(
136-
queryFn: (
137-
q: InitialQueryBuilder
138-
) => Collection<TResult, TKey, TUtils> | undefined | null,
139-
deps?: Array<unknown>
140-
): DisabledQueryError
141-
142116
// Overload 1: Accept query function that always returns QueryBuilder
143117
export function useLiveSuspenseQuery<TContext extends Context>(
144118
queryFn: (q: InitialQueryBuilder) => QueryBuilder<TContext>,
@@ -185,6 +159,33 @@ export function useLiveSuspenseQuery<
185159
collection: Collection<TResult, TKey, TUtils> & SingleResult
186160
}
187161

162+
// "Poison pill" overloads - catch disabled queries and show custom compile-time error
163+
// These MUST come AFTER the specific overloads so TypeScript tries them last
164+
export function useLiveSuspenseQuery<TContext extends Context>(
165+
queryFn: (
166+
q: InitialQueryBuilder
167+
) => QueryBuilder<TContext> | undefined | null,
168+
deps?: Array<unknown>
169+
): DisabledQueryError
170+
171+
export function useLiveSuspenseQuery<TContext extends Context>(
172+
queryFn: (
173+
q: InitialQueryBuilder
174+
) => LiveQueryCollectionConfig<TContext> | undefined | null,
175+
deps?: Array<unknown>
176+
): DisabledQueryError
177+
178+
export function useLiveSuspenseQuery<
179+
TResult extends object,
180+
TKey extends string | number,
181+
TUtils extends Record<string, any>,
182+
>(
183+
queryFn: (
184+
q: InitialQueryBuilder
185+
) => Collection<TResult, TKey, TUtils> | undefined | null,
186+
deps?: Array<unknown>
187+
): DisabledQueryError
188+
188189
// Implementation - uses useLiveQuery internally and adds Suspense logic
189190
export function useLiveSuspenseQuery(
190191
configOrQueryOrCollection: any,

0 commit comments

Comments
 (0)