Skip to content

Commit 8a29874

Browse files
committed
fix(db): infer type in coalesce() instead of returning BasicExpression<any>
Previously coalesce() always returned BasicExpression<any>, losing type information. Now it uses a generic to infer the non-nullable union of all argument types, matching the semantic of SQL COALESCE. Fixes #1341
1 parent 168717a commit 8a29874

3 files changed

Lines changed: 10 additions & 3 deletions

File tree

.changeset/fix-coalesce-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tanstack/db": patch
3+
---
4+
5+
fix(db): infer type in coalesce() instead of returning BasicExpression<any>

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,13 @@ export function concat(
285285
)
286286
}
287287

288-
export function coalesce(...args: Array<ExpressionLike>): BasicExpression<any> {
288+
export function coalesce<T extends Array<ExpressionLike>>(
289+
...args: T
290+
): BasicExpression<NonNullable<ExtractType<T[number]>>> {
289291
return new Func(
290292
`coalesce`,
291293
args.map((arg) => toExpression(arg)),
292-
)
294+
) as BasicExpression<NonNullable<ExtractType<T[number]>>>
293295
}
294296

295297
export function add<T1 extends ExpressionLike, T2 extends ExpressionLike>(

packages/db/tests/query/builder/callback-types.test-d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ describe(`Query Builder Callback Types`, () => {
150150
BasicExpression<number>
151151
>()
152152
expectTypeOf(coalesce(user.name, `Unknown`)).toEqualTypeOf<
153-
BasicExpression<any>
153+
BasicExpression<string>
154154
>()
155155

156156
return {

0 commit comments

Comments
 (0)