Skip to content

Commit a8c2ddc

Browse files
committed
refactor(query-core): consolidate NonFunction type into NonFunctionGuard
Moved NonFunction type from utils.ts to types.ts as NonFunctionGuard with improved documentation. Applied the type guard consistently to placeholderData and updated all references and documentation to use the new name.
1 parent 8a6db1b commit a8c2ddc

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

packages/query-core/src/types.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,14 @@ export type QueryFunctionContext<
166166

167167
export type InitialDataFunction<T> = () => T | undefined
168168

169+
/**
170+
* `NonFunctionGuard<T>` ensures T is not a function type.
171+
*
172+
* If T is a function, it resolves to `never`, effectively removing T
173+
* from unions and preventing ambiguity in value-or-function patterns.
174+
*/
175+
export type NonFunctionGuard<T> = T extends Function ? never : T
176+
169177
type PlaceholderDataFunction<
170178
TQueryFnData = unknown,
171179
TError = DefaultError,
@@ -422,8 +430,13 @@ export interface QueryObserverOptions<
422430
* If set, this value will be used as the placeholder data for this particular query observer while the query is still in the `loading` data and no initialData has been provided.
423431
*/
424432
placeholderData?:
425-
| TQueryData
426-
| PlaceholderDataFunction<TQueryData, TError, TQueryData, TQueryKey>
433+
| NonFunctionGuard<TQueryData>
434+
| PlaceholderDataFunction<
435+
NonFunctionGuard<TQueryData>,
436+
TError,
437+
NonFunctionGuard<TQueryData>,
438+
TQueryKey
439+
>
427440

428441
_optimisticResults?: 'optimistic' | 'isRestoring'
429442

packages/query-core/src/utils.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
FetchStatus,
77
MutationKey,
88
MutationStatus,
9+
NonFunctionGuard,
910
QueryFunction,
1011
QueryKey,
1112
QueryOptions,
@@ -66,15 +67,8 @@ export interface MutationFilters<
6667
status?: MutationStatus
6768
}
6869

69-
/**
70-
* Utility type that excludes function types from T.
71-
* If T is a function, it resolves to `never`, effectively removing T
72-
* from unions and preventing ambiguity in value-or-function patterns.
73-
*/
74-
export type NonFunction<T> = T extends (...args: Array<any>) => any ? never : T
75-
7670
export type Updater<TInput, TOutput> =
77-
| NonFunction<TOutput>
71+
| NonFunctionGuard<TOutput>
7872
| ((input: TInput) => TOutput)
7973

8074
export type QueryTypeFilter = 'all' | 'active' | 'inactive'
@@ -94,7 +88,7 @@ export function noop() {}
9488
* throughout the codebase and provides a clean way to handle the common pattern where
9589
* options can be static values or dynamic functions.
9690
*
97-
* The NonFunction<T> constraint eliminates ambiguity by ensuring T can never be a function
91+
* The NonFunctionGuard<T> constraint eliminates ambiguity by ensuring T can never be a function
9892
* type. This makes the value-or-function pattern type-safe and unambiguous.
9993
*
10094
* The function provides two overloads: one that includes `| undefined` for optional values
@@ -140,21 +134,21 @@ export function noop() {}
140134
* ```
141135
*/
142136
export function resolveOption<T, TArgs extends Array<any>>(
143-
valueOrFn: NonFunction<T> | ((...args: TArgs) => T) | undefined,
137+
valueOrFn: NonFunctionGuard<T> | ((...args: TArgs) => T) | undefined,
144138
...args: TArgs
145139
): T | undefined
146140
// Overload for when value is guaranteed to be present
147141
export function resolveOption<T, TArgs extends Array<any>>(
148-
valueOrFn: NonFunction<T> | ((...args: TArgs) => T),
142+
valueOrFn: NonFunctionGuard<T> | ((...args: TArgs) => T),
149143
...args: TArgs
150144
): T
151145
// Implementation
152146
export function resolveOption<T, TArgs extends Array<any>>(
153-
valueOrFn: NonFunction<T> | ((...args: TArgs) => T) | undefined,
147+
valueOrFn: NonFunctionGuard<T> | ((...args: TArgs) => T) | undefined,
154148
...args: TArgs
155149
): T | undefined {
156150
if (typeof valueOrFn === 'function') {
157-
// Because of our NonFunction<T> utility, TypeScript now correctly
151+
// Because of our NonFunctionGuard<T> utility, TypeScript now correctly
158152
// infers that if valueOrFn is a function, it must be the producer `(...args: TArgs) => T`.
159153
return (valueOrFn as (...args: TArgs) => T)(...args)
160154
}

0 commit comments

Comments
 (0)