Skip to content

fix(db): infer non-nullable return type from first arg in coalesce()#1342

Open
sleitor wants to merge 2 commits intoTanStack:mainfrom
sleitor:fix-1341
Open

fix(db): infer non-nullable return type from first arg in coalesce()#1342
sleitor wants to merge 2 commits intoTanStack:mainfrom
sleitor:fix-1341

Conversation

@sleitor
Copy link
Contributor

@sleitor sleitor commented Mar 8, 2026

Problem

coalesce() was typed as (...args: Array<ExpressionLike>): BasicExpression<any>, which loses all type information when used in .select().

Fixes #1341

Solution

Update the signature to infer T from the first argument so the return type is BasicExpression<NonNullable<ExtractType<T>>>. Subsequent args remain typed as ExpressionLike to support mixed-type calls like coalesce(count(col), 0).

// Before
const a = coalesce('hello', 'world') // BasicExpression<any>
const b = coalesce(1, 2)             // BasicExpression<any>

// After
const a = coalesce('hello', 'world') // BasicExpression<string>
const b = coalesce(1, 2)             // BasicExpression<number>

Changes

  • packages/db/src/query/builder/functions.ts: Updated coalesce signature with generic T inferred from first arg
  • Updated type test expectation from BasicExpression<any> to BasicExpression<string>
  • Added a new type inference test case

@changeset-bot
Copy link

changeset-bot bot commented Mar 8, 2026

🦋 Changeset detected

Latest commit: 348a281

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
@tanstack/db Patch
@tanstack/angular-db Patch
@tanstack/electric-db-collection Patch
@tanstack/offline-transactions Patch
@tanstack/powersync-db-collection Patch
@tanstack/query-db-collection Patch
@tanstack/react-db Patch
@tanstack/rxdb-db-collection Patch
@tanstack/solid-db Patch
@tanstack/svelte-db Patch
@tanstack/trailbase-db-collection Patch
@tanstack/vue-db Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 8, 2026

More templates

@tanstack/angular-db

npm i https://pkg.pr.new/@tanstack/angular-db@1342

@tanstack/db

npm i https://pkg.pr.new/@tanstack/db@1342

@tanstack/db-ivm

npm i https://pkg.pr.new/@tanstack/db-ivm@1342

@tanstack/electric-db-collection

npm i https://pkg.pr.new/@tanstack/electric-db-collection@1342

@tanstack/offline-transactions

npm i https://pkg.pr.new/@tanstack/offline-transactions@1342

@tanstack/powersync-db-collection

npm i https://pkg.pr.new/@tanstack/powersync-db-collection@1342

@tanstack/query-db-collection

npm i https://pkg.pr.new/@tanstack/query-db-collection@1342

@tanstack/react-db

npm i https://pkg.pr.new/@tanstack/react-db@1342

@tanstack/rxdb-db-collection

npm i https://pkg.pr.new/@tanstack/rxdb-db-collection@1342

@tanstack/solid-db

npm i https://pkg.pr.new/@tanstack/solid-db@1342

@tanstack/svelte-db

npm i https://pkg.pr.new/@tanstack/svelte-db@1342

@tanstack/trailbase-db-collection

npm i https://pkg.pr.new/@tanstack/trailbase-db-collection@1342

@tanstack/vue-db

npm i https://pkg.pr.new/@tanstack/vue-db@1342

commit: 1b8b2c6

…n<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 TanStack#1341
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

coalesce() returns BasicExpression<any>, losing type inference

1 participant