From 8a29874d9f8b7fe5a642ca3bda8c2a06ec58a454 Mon Sep 17 00:00:00 2001 From: Dmitrii Troitskii Date: Sun, 8 Mar 2026 18:35:27 +0000 Subject: [PATCH 1/2] fix(db): infer type in coalesce() instead of returning BasicExpression Previously coalesce() always returned BasicExpression, 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 --- .changeset/fix-coalesce-type.md | 5 +++++ packages/db/src/query/builder/functions.ts | 6 ++++-- packages/db/tests/query/builder/callback-types.test-d.ts | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 .changeset/fix-coalesce-type.md diff --git a/.changeset/fix-coalesce-type.md b/.changeset/fix-coalesce-type.md new file mode 100644 index 000000000..c6e8fefd0 --- /dev/null +++ b/.changeset/fix-coalesce-type.md @@ -0,0 +1,5 @@ +--- +"@tanstack/db": patch +--- + +fix(db): infer type in coalesce() instead of returning BasicExpression diff --git a/packages/db/src/query/builder/functions.ts b/packages/db/src/query/builder/functions.ts index 887c1468d..b11aea1fc 100644 --- a/packages/db/src/query/builder/functions.ts +++ b/packages/db/src/query/builder/functions.ts @@ -285,11 +285,13 @@ export function concat( ) } -export function coalesce(...args: Array): BasicExpression { +export function coalesce>( + ...args: T +): BasicExpression>> { return new Func( `coalesce`, args.map((arg) => toExpression(arg)), - ) + ) as BasicExpression>> } export function add( diff --git a/packages/db/tests/query/builder/callback-types.test-d.ts b/packages/db/tests/query/builder/callback-types.test-d.ts index 0e901b7fb..b1565ea32 100644 --- a/packages/db/tests/query/builder/callback-types.test-d.ts +++ b/packages/db/tests/query/builder/callback-types.test-d.ts @@ -150,7 +150,7 @@ describe(`Query Builder Callback Types`, () => { BasicExpression >() expectTypeOf(coalesce(user.name, `Unknown`)).toEqualTypeOf< - BasicExpression + BasicExpression >() return { From 348a28142a2d0263b8f457f1c4b4ab686b9a09ca Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Sun, 8 Mar 2026 18:36:35 +0000 Subject: [PATCH 2/2] ci: apply automated fixes --- .changeset/fix-coalesce-type.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/fix-coalesce-type.md b/.changeset/fix-coalesce-type.md index c6e8fefd0..3341b355a 100644 --- a/.changeset/fix-coalesce-type.md +++ b/.changeset/fix-coalesce-type.md @@ -1,5 +1,5 @@ --- -"@tanstack/db": patch +'@tanstack/db': patch --- fix(db): infer type in coalesce() instead of returning BasicExpression