Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(*): change noop function return type from undefined to void #8299

Merged
merged 9 commits into from
Nov 19, 2024
3 changes: 2 additions & 1 deletion packages/angular-query-experimental/src/providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { QueryClient, onlineManager } from '@tanstack/query-core'
import { isPlatformBrowser } from '@angular/common'
import { isDevMode } from './util/is-dev-mode/is-dev-mode'
import { noop } from './util'
import type { EnvironmentProviders, Provider } from '@angular/core'
import type {
DevtoolsButtonPosition,
Expand Down Expand Up @@ -251,7 +252,7 @@ export function withDevtools(
provide: ENVIRONMENT_INITIALIZER,
multi: true,
useFactory: () => {
if (!isPlatformBrowser(inject(PLATFORM_ID))) return () => {}
if (!isPlatformBrowser(inject(PLATFORM_ID))) return noop
const injector = inject(Injector)
const options = computed(() =>
runInInjectionContext(injector, () => optionsFn?.() ?? {}),
Expand Down
4 changes: 2 additions & 2 deletions packages/query-core/src/queryObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { pendingThenable } from './thenable'
import {
isServer,
isValidTimeout,
noop,
replaceData,
resolveEnabled,
resolveStaleTime,
returnUndefined,
shallowEqualObjects,
timeUntilStale,
} from './utils'
Expand Down Expand Up @@ -349,7 +349,7 @@ export class QueryObserver<
)

if (!fetchOptions?.throwOnError) {
promise = promise.catch(noop)
promise = promise.catch(returnUndefined)
}

return promise
Expand Down
6 changes: 3 additions & 3 deletions packages/query-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ export type QueryTypeFilter = 'all' | 'active' | 'inactive'

export const isServer = typeof window === 'undefined' || 'Deno' in globalThis

export function noop(): undefined {
return undefined
}
export function noop(): void {}

export const returnUndefined = noop as () => undefined
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would overloads also work?

export function noop(): undefined
export function noop(): void
export function noop() {}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion!🙌 reflected in 454956e

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found an interesting TypeScript behavior.

// Works: No type error when used in Promise.catch()
// Type inference: function noop(): void (+1 overload)
export function noop(): void
export function noop(): undefined
export function noop() {}

// Doesn't work: Type error when used in Promise<TQueryData | undefined>.catch()
// Type inference: function noop(): undefined (+1 overload)
export function noop(): undefined
export function noop(): void
export function noop() {}


export function functionalUpdate<TInput, TOutput>(
updater: Updater<TInput, TOutput>,
Expand Down
4 changes: 2 additions & 2 deletions packages/query-sync-storage-persister/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { noop } from './utils'
import { noop, returnUndefined } from './utils'
import type {
PersistRetryer,
PersistedClient,
Expand Down Expand Up @@ -88,7 +88,7 @@ export function createSyncStoragePersister({

return {
persistClient: noop,
restoreClient: () => undefined,
restoreClient: returnUndefined,
removeClient: noop,
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/query-sync-storage-persister/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export function noop(): void {}

export const returnUndefined = noop as () => undefined
4 changes: 2 additions & 2 deletions packages/svelte-query/src/createBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { derived, get, readable } from 'svelte/store'
import { notifyManager } from '@tanstack/query-core'
import { useIsRestoring } from './useIsRestoring.js'
import { useQueryClient } from './useQueryClient.js'
import { isSvelteStore } from './utils.js'
import { isSvelteStore, noop } from './utils.js'
import type {
QueryClient,
QueryKey,
Expand Down Expand Up @@ -66,7 +66,7 @@ export function createBaseQuery<
QueryObserverResult<TData, TError>
>(isRestoring, ($isRestoring, set) => {
const unsubscribe = $isRestoring
? () => undefined
? noop
: observer.subscribe(notifyManager.batchCalls(set))
observer.updateResult()
return unsubscribe
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte-query/src/createQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { QueriesObserver, notifyManager } from '@tanstack/query-core'
import { derived, get, readable } from 'svelte/store'
import { useIsRestoring } from './useIsRestoring.js'
import { useQueryClient } from './useQueryClient.js'
import { isSvelteStore } from './utils.js'
import { isSvelteStore, noop } from './utils.js'
import type { Readable } from 'svelte/store'
import type { StoreOrVal } from './types.js'
import type {
Expand Down Expand Up @@ -253,7 +253,7 @@ export function createQueries<

const result = derived([isRestoring], ([$isRestoring], set) => {
const unsubscribe = $isRestoring
? () => undefined
? noop
: observer.subscribe(notifyManager.batchCalls(set))

return () => unsubscribe()
Expand Down
Loading