Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/query-core-noinfer-builtin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@tanstack/query-core': patch
'@tanstack/vue-query': patch
---

fix(query-core): drop the custom `NoInfer<T>` re-export and rely on TypeScript's built-in `NoInfer` (TS ≥ 5.4) so `NoInfer<X[K]>` stays assignable to `X[K]` in generic contexts (fixes #9937)
7 changes: 1 addition & 6 deletions packages/preact-query/src/useQuery.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { QueryObserver } from '@tanstack/query-core'
import type {
DefaultError,
NoInfer,
QueryClient,
QueryKey,
} from '@tanstack/query-core'
import type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'

import type {
DefinedInitialDataOptions,
Expand Down
1 change: 0 additions & 1 deletion packages/query-core/src/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import type {
MutationKey,
MutationObserverOptions,
MutationOptions,
NoInfer,
OmitKeyof,
QueryClientConfig,
QueryKey,
Expand Down
2 changes: 0 additions & 2 deletions packages/query-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ export type Override<TTargetA, TTargetB> = {
: TTargetA[AKey]
}

export type NoInfer<T> = [T][T extends any ? 0 : never]

export interface Register {
// defaultError: Error
// queryMeta: Record<string, unknown>
Expand Down
48 changes: 48 additions & 0 deletions packages/react-query/src/__tests__/useQuery.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,52 @@ describe('useQuery', () => {
})
})
})

describe('generic indexed access TData', () => {
// https://github.com/TanStack/query/issues/9937
it('should be assignable back to its source indexed type when passed to a generic function parameter', () => {
enum DataType {
Account = 'account',
Product = 'product',
}

interface Account {
name: string
}
interface Product {
code: string
}

type DataTypeToEntity = {
[DataType.Account]: Account
[DataType.Product]: Product
}

const getData = <TDataType extends DataType>(
_dataType: TDataType,
): Promise<DataTypeToEntity[TDataType]> =>
Promise.resolve({} as DataTypeToEntity[TDataType])

const getLabel = <TDataType extends DataType>(
_dataType: TDataType,
_data: DataTypeToEntity[TDataType],
) => 'test'

function Test<TDataType extends DataType>(props: {
dataType: TDataType
}) {
const { data } = useQuery({
queryKey: ['test'],
queryFn: () => getData(props.dataType),
})

// Regression guard: this call must compile. With the previous
// hand-rolled NoInfer, `data` failed to flow back into the generic
// indexed-access parameter `DataTypeToEntity[TDataType]`.
return data ? getLabel(props.dataType, data) : null
}

expectTypeOf(Test).toBeFunction()
})
})
})
7 changes: 1 addition & 6 deletions packages/react-query/src/useQuery.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
'use client'
import { QueryObserver } from '@tanstack/query-core'
import { useBaseQuery } from './useBaseQuery'
import type {
DefaultError,
NoInfer,
QueryClient,
QueryKey,
} from '@tanstack/query-core'
import type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'
import type {
DefinedUseQueryResult,
UseQueryOptions,
Expand Down
3 changes: 1 addition & 2 deletions packages/vue-query/src/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import type {
MutationFilters,
MutationKey,
MutationObserverOptions,
NoInfer,
OmitKeyof,
QueryFilters,
QueryKey,
Expand Down Expand Up @@ -125,7 +124,7 @@ export class QueryClient extends QC {
updater: Updater<TData | undefined, TData | undefined>,
options: MaybeRefDeep<SetDataOptions> = {},
): NoInfer<TData> | undefined {
return super.setQueryData(
return super.setQueryData<TData>(
cloneDeepUnref(queryKey),
updater,
cloneDeepUnref(options),
Expand Down
Loading