NoInfer around TData causes TS error with object type #9937
Send to my agentDescribe the bug#
We recently updated @tanstack/react-query to version 5.90.11 and it introduced type errors, we believe related to the change in #8683
We have a map from an enum value to an interface so that we can have generic data loaders with the correct data type returned (simplified example in the attached TS Playground link) and then use that elsewhere to make other functions generic.
We are now getting this TS error:
Argument of type 'NoInfer<DataTypeToEntity[DT]>' is not assignable to parameter of type 'DataTypeToEntity[DT]'.
Type '[DataTypeToEntity[DT]]' is not assignable to type 'DataTypeToEntity'.
I created a very simple reproduction of the issue with NoInfer that also shows the issue not happening with the built-in NoInfer from TS 5.4 - https://www.typescriptlang.org/play/?#code/KYOwrgtgBAIghgFzgFQJ4AdhQN4CgpQCCAxsQPZggJQC8UA5HKRVfQDT5QAKATmQCZhi1OvXR9Bw+rgC+uXAEsqwHgDMmWEuUrU8BEHAjAAXFADOCHkoDmAblnylCFeuJZeAobs7l+J85Y29nK4wAAe6GQ81AgYWPBIaJjIZACiVAqxtDicANoJKHEAdFosCAC6pqU69gT5iIWYRR6SFaYtXg64APTdUMgAFgpmUMNQCANYsZhQqnzQAEoAirjTWAByZACSIKoqAEwAPMgAfNm5yOUXUOHOIPwjcCCoUAD8UAAMUKYgwABuKnK9lw5BAFnGwHBdEOMGQNzCdwesAaSWAJwAFPwUXFTLC2FAsUhTJsdnseDDsck0hlYvlLicAJS0M56KCg8FwMxmBTWX78XGU4ApdIITKoOnlbKEuDBeTsmKQhD7bIwuG3UBIgqojHS1G45D46XE7a7A4UxJxYU08Ww8qM5k5AjyqCc7m84D85EWqkisUSqUNYJAA
Also doesn't seem to happen with a simpler type NoInfer<T> = [T] extends [T] ? T : never approach.
I know this is very much an edge-case but I'm struggling to find a workaround other than casting or passing generics into useQuery, neither of which are ideal.
Your minimal, reproducible example#
Steps to reproduce#
The error is shown at the bottom of the TS Playground link, where we attempt to assign data to a type that should match fine.
Expected behavior#
The type should be fine because NoInfer<SomeType> should be able to be assigned to SomeType.
How often does this bug happen?#
None
Screenshots or Videos#
No response
Platform#
@tanstack/react-query: v5.90.11
TypeScript: 5.7.2 (our codebase) / 5.9.3 (TS Playground)
Tanstack Query adapter#
None
TanStack Query version#
5.90.11
TypeScript version#
5.9.3
Additional context#
No response
TkDodo added labelstypes
It does seem to cause some errors in
packages/query-core/src/__tests__/queryClient.test-d.tsxthough, and other popularNoInferimplementations seem to cause errors in one place or another. 🤔yeah I just double checked and that implementation doesn’t really do anything. I think this will have to wait until the next major, sorry.
TkDodo closed this
@TkDodo I am seeing type issues in my project after updating to the latest release that includes #10593
NoInferis now preventing data returned fromuseQueryfrom being narrowed. For instance:const isMyFirstResult(value: unknown): value is MyFirstResult => { ... }; // queryFn return type is MyFirstResult | MySecondResult const { data } = useQuery(...); // Before: const secondResult = isMyFirstResult(data) ? undefined : data; // ^ output type is MySecondResult | undefined // After: const secondResult = isMyFirstResult(data) ? undefined : data; // ^ output type is NoInfer<MyFirstResult | MySecondResult> | undefinedThis breaks usage anywhere the narrowed data is expected when used.
Let me know if this should be a new issue!
@TkDodo I am seeing type issues in my project after updating to the latest release that includes #10593
NoInferis now preventing data returned fromuseQueryfrom being narrowed. For instance:const isMyFirstResult(value: unknown): value is MyFirstResult => { ... };
// queryFn return type is MyFirstResult | MySecondResult const { data } = useQuery(...);
// Before: const secondResult = isMyFirstResult(data) ? undefined : data; // ^ output type is MySecondResult | undefined
// After: const secondResult = isMyFirstResult(data) ? undefined : data; // ^ output type is NoInfer<MyFirstResult | MySecondResult> | undefined This breaks usage anywhere the narrowed data is expected when used.
Let me know if this should be a new issue!
please open a new issue with a typescript playground if possible that shows the issue
Sign in to join this conversation.
I think we can try switching to your simpler version of
NoInfer- I don’t see any failing type-tests that we have with that.For the next major, we’ll bump the types dependency and use the built-in version.
Do you want to contribute it?