Describe the bug
Issue: @tanstack/react-query types break with TypeScript nodenext module resolution (TS2883)
Environment
- TypeScript: 6.0.3
- @tanstack/react-query: 5.101.0
- @tanstack/query-core: 5.101.0
- Module: nodenext
- ModuleResolution: nodenext
Description
When using TypeScript with module: "nodenext", any exported function that infers its return type from @tanstack/react-query hooks fails with TS2883:
The inferred type of 'useChunkTag' cannot be named without a reference to
'QueryObserverResult' from '../../../../../../node_modules/@tanstack/query-core/build/modern/_tsup-dts-rollup.cjs'.
Root Cause
The issue is in how the type declaration files are structured:
ESM entry (index.d.ts):
export { QueryObserverResult } from './_tsup-dts-rollup.js'; // ✅ Correct .js extension
CJS entry (index.d.cts):
export { QueryObserverResult } from './_tsup-dts-rollup.cjs'; // CJS path
When TypeScript generates .d.ts declarations for functions with inferred return types in nodenext mode, it incorrectly resolves through the CJS path (_tsup-dts-rollup.cjs) instead of the ESM path (_tsup-dts-rollup.js). Since .cjs is not a valid ESM import path, TypeScript cannot generate a valid type reference and throws TS2883.
Reproduction
// hooks.ts
import { useQuery } from "@tanstack/react-query";
// This function has an inferred return type that depends on @tanstack types
export function useMyHook() {
const query = useQuery({ queryKey: ['test'], queryFn: () => 'hello' });
return { data: query.data };
}
With tsconfig.json:
{
"compilerOptions": {
"module": "nodenext",
"moduleResolution": "nodenext",
"declaration": true
}
}
Running tsc will produce TS2883 errors.
Workaround
Explicitly declare return types to avoid TypeScript inference:
export function useMyHook(): { data: string | undefined } {
const query = useQuery({ queryKey: ['test'], queryFn: () => 'hello' });
return { data: query.data };
}
Expected Behavior
Types should work correctly with nodenext module resolution, as this is the recommended setting for Node.js applications and libraries.
Your minimal, reproducible example
.
Steps to reproduce
// hooks.ts
import { useQuery } from "@tanstack/react-query";
// This function has an inferred return type that depends on @tanstack types
export function useMyHook() {
const query = useQuery({ queryKey: ['test'], queryFn: () => 'hello' });
return { data: query.data };
}
With tsconfig.json:
{
"compilerOptions": {
"module": "nodenext",
"moduleResolution": "nodenext",
"declaration": true
}
}
Running tsc will produce TS2883 errors.
Expected behavior
Types should work correctly with nodenext module resolution, as this is the recommended setting for Node.js applications and libraries.
How often does this bug happen?
None
Screenshots or Videos
No response
Platform
Windows 11
Tanstack Query adapter
None
TanStack Query version
v5.101.0
TypeScript version
v6.0.3
Additional context
No response
Describe the bug
Issue: @tanstack/react-query types break with TypeScript
nodenextmodule resolution (TS2883)Environment
Description
When using TypeScript with
module: "nodenext", any exported function that infers its return type from@tanstack/react-queryhooks fails with TS2883:Root Cause
The issue is in how the type declaration files are structured:
ESM entry (
index.d.ts):CJS entry (
index.d.cts):When TypeScript generates
.d.tsdeclarations for functions with inferred return types innodenextmode, it incorrectly resolves through the CJS path (_tsup-dts-rollup.cjs) instead of the ESM path (_tsup-dts-rollup.js). Since.cjsis not a valid ESM import path, TypeScript cannot generate a valid type reference and throws TS2883.Reproduction
With
tsconfig.json:{ "compilerOptions": { "module": "nodenext", "moduleResolution": "nodenext", "declaration": true } }Running
tscwill produce TS2883 errors.Workaround
Explicitly declare return types to avoid TypeScript inference:
Expected Behavior
Types should work correctly with
nodenextmodule resolution, as this is the recommended setting for Node.js applications and libraries.Your minimal, reproducible example
.
Steps to reproduce
With
tsconfig.json:{ "compilerOptions": { "module": "nodenext", "moduleResolution": "nodenext", "declaration": true } }Running
tscwill produce TS2883 errors.Expected behavior
Types should work correctly with
nodenextmodule resolution, as this is the recommended setting for Node.js applications and libraries.How often does this bug happen?
None
Screenshots or Videos
No response
Platform
Windows 11
Tanstack Query adapter
None
TanStack Query version
v5.101.0
TypeScript version
v6.0.3
Additional context
No response