Now I have a complete picture. Let me write the investigation.
When consumers use declaration: true in their tsconfig.json (common with TS project references), TypeScript emits errors like "The inferred type of 'xyz' cannot be named without a reference to .../node_modules/@tanstack/db/dist/esm/query/builder/types". This happens specifically when using query joins and select. The root problem is that several types used in public method return signatures are not re-exported from the package's public API.
The BaseQueryBuilder class (packages/db/src/query/builder/index.ts) uses numerous types from ./types.ts in its public method return types, but only a small subset of those types are re-exported through the public API chain.
The export chain today:
| File | Types re-exported |
|---|---|
packages/db/src/query/builder/index.ts:911-917 |
Context, Source, GetResult, RefLeaf as Ref, InferResultType |
packages/db/src/query/index.ts:4-15 |
Same + ExtractContext, QueryResult, QueryBuilder, InitialQueryBuilder |
packages/db/src/index.ts:11 |
Re-exports everything via export * from './query/index.js' |
Types used in public return types but NOT exported:
| Missing Type | Used in method |
|---|---|
SchemaFromSource<T> |
from() return type (line 139-140), all join() variants |
MergeContextWithJoinType<...> |
join() return type (line 188-189), leftJoin(), rightJoin(), innerJoin(), fullJoin() |
MergeContextForJoinCallback<...> |
join() callback parameter (line 185) |
ResultTypeFromSelect<T> |
select() return type (line 475) |
WithResult<...> |
select() return type (line 475) |
JoinOnCallback<T> |
join() parameter type (line 184) |
RefsForContext<T> |
select() callback parameter (line 474), where(), orderBy(), groupBy() |
WhereCallback<T> |
where() parameter type |
OrderByCallback<T> |
orderBy() parameter type |
SelectObject<T> |
select() constraint |
Prettify<T> |
Used transitively in multiple return types |
When TypeScript tries to generate .d.ts files for a consumer, it cannot name these types because they aren't accessible through the package's public exports — hence the error.
Add the missing types to the re-export chain at packages/db/src/query/builder/index.ts:910-917:
export type {
Context,
Source,
GetResult,
RefLeaf as Ref,
InferResultType,
// Add these to fix declaration emit:
SchemaFromSource,
MergeContextWithJoinType,
MergeContextForJoinCallback,
ResultTypeFromSelect,
WithResult,
JoinOnCallback,
RefsForContext,
WhereCallback,
OrderByCallback,
GroupByCallback,
SelectObject,
Prettify,
ContextSchema,
ApplyJoinOptionalityToMergedSchema,
} from './types.js'Then similarly update packages/db/src/query/index.ts:4-15 and verify they flow through packages/db/src/index.ts (which already uses export * so it should pick them up automatically once query/index.ts exports them).
Verification approach: Set up a test project with declaration: true and composite: true in tsconfig, import the query builder, use from() + join() + select(), and confirm tsc --noEmit passes.
Small — This is purely an additive change to type re-exports. No runtime code changes needed. The main work is identifying which types to export and verifying the fix with the reproduction case.
Thanks for the clear repro, @marbemac! This is caused by several types (like
SchemaFromSource,MergeContextWithJoinType,WithResult, etc.) being used in public method return types ofBaseQueryBuilderbut not re-exported from the package's public API. Whendeclaration: trueis set, TypeScript can't generate valid.d.tsfiles because it can't reference those internal types. The fix is to add the missing type re-exports atpackages/db/src/query/builder/index.tsand propagate them through the export chain. This is similar to the issue TanStack Query had with unexported symbols (#8453 in that repo).