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

Conversation

kangju2000
Copy link
Contributor

@kangju2000 kangju2000 commented Nov 16, 2024

I modify the return type of noop function from () => {} to noop():void for type consistency.

[AS-IS]

  • noop function returns undefined while methods in query-core return Promise<void>
  • Methods using promise.catch(noop) have inconsistent return types with their declarations
  • #executeFetch method has the same issue where Promise<TQueryData | undefined> meets promise.catch(noop)

[TO-BE]

  • Explicitly type noop function to return void
  • Replace promise.catch(noop) with promise.catch(() => undefined) in #executeFetch to match its return type
  • Apply noop():void to other promise chains in query-core where return type is Promise<void>

@TkDodo
Copy link
Collaborator

TkDodo commented Nov 17, 2024

didn’t you just literally change the same thing @manudeli ?

I’m confused

@manudeli
Copy link
Contributor

didn’t you just literally change the same thing @manudeli ?

I’m confused

@kangju2000 check this comment #8294 (comment) please

@kangju2000
Copy link
Contributor Author

kangju2000 commented Nov 17, 2024

Sorry for not checking PR #8294 (comment).
I tried to change it to void for semantic clarity of "no operation", but as discussed there, we need undefined for practical type safety in Promise chains. I agree with keeping noop(): undefined.

What do you think about adding this comment to help future contributors understand the design decision? @TkDodo @manudeli

/**
 * No-operation function that returns undefined for type safety in Promise chains
 */
function noop(): undefined {
  return undefined
}

@manudeli
Copy link
Contributor

@TkDodo Since noop returning void and functions returning undefined are widely used in all packages,

How about providing both function noop(): void {} and function returnUndefined(): undefined {} in @tanstack/query-core and using them in all packages such as @tanstack/react-query?

@saul-atomrigs
Copy link
Contributor

Quick, simple suggestion:

export function noop(): void | undefined {}

Is this ok?

@TkDodo
Copy link
Collaborator

TkDodo commented Nov 18, 2024

That's fine if TypeScript likes it everywhere 👍

@saul-atomrigs
Copy link
Contributor

If that makes typescript unhappy, we may just let it be inferred ..

export function noop(): {
  return undefined
}

up to you guys 😅

@kangju2000
Copy link
Contributor Author

kangju2000 commented Nov 18, 2024

@TkDodo Separate noop and returnUndefined functions as reflected in f2820ac a46bd67

@manudeli
Copy link
Contributor

manudeli commented Nov 18, 2024

@TkDodo Separate noop and returnUndefined functions as reflected in f2820ac

Looking at this comment(#8294 (comment)), it seems that TkDodo did not want to implement noop and returnUndefined separately and include them in the package bundle since they are the same implementation in JavaScript.

So, how about implementing it as follows?

// @tanstack/query-core
export function noop(): void {}
export const returnUndefined = noop as () => undefined // re-assert type

// @tanstack/react-query just use @tanstack/query-core's noop, returnUndefined
import { noop, returnUndefined } from '@tanstack/query-core'

// @tanstack/vue-query just use @tanstack/query-core's noop, returnUndefined
import { noop, returnUndefined } from '@tanstack/query-core'

// @tanstack/svelte-query just use @tanstack/query-core's noop, returnUndefined
import { noop, returnUndefined } from '@tanstack/query-core'

if don't want to export noop, returnUndefined in @tanstack/query-core, How about implementing like below

// @tanstack/query-core
export function noop(): void {}
export const returnUndefined = noop as () => undefined // re-assert type

// @tanstack/react-query
export function noop(): void {}
export const returnUndefined = noop as () => undefined // re-assert type

// @tanstack/vue-query
export function noop(): void {}
export const returnUndefined = noop as () => undefined // re-assert type

// @tanstack/svelte-query
export function noop(): void {}
export const returnUndefined = noop as () => undefined // re-assert type

@kangju2000
Copy link
Contributor Author

kangju2000 commented Nov 18, 2024

Thank you for the good suggestion! From a bundle size optimization perspective, type re-assertion seems better. I'll try changing it to export const returnUndefined = noop as () => undefined as suggested, and if there are any readability issues, we can consider rolling back to the previous approach.

Additionally, these utility functions seem to fall outside the core responsibilities of @tanstack/query-core and would be better handled through dedicated utility libraries.

Comment on lines 71 to 73
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() {}

Copy link

nx-cloud bot commented Nov 19, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 9a6220d. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets

Sent with 💌 from NxCloud.

@TkDodo
Copy link
Collaborator

TkDodo commented Nov 19, 2024

it doesn’t work. please make sure locally that everything compiles properly.

@manudeli
Copy link
Contributor

@kangju2000 You have to run script test:types https://github.com/TanStack/query/blob/main/package.json#L22 in root

@kangju2000
Copy link
Contributor Author

@TkDodo

Sorry for the hassle. I've added type overrides for noop only where necessary. Using the overridden noop in the following code causes errors, so I kept it unchanged in solid-query.

// packages/solid-query/src/createQueries.ts:309
let unsubscribe = noop
createComputed<() => void>((cleanup) => {
  cleanup?.()
  unsubscribe = isRestoring() ? noop : subscribeToObserver()
  return () => queueMicrotask(unsubscribe)
})

Copy link

pkg-pr-new bot commented Nov 19, 2024

Open in Stackblitz

More templates

@tanstack/angular-query-devtools-experimental

pnpm add https://pkg.pr.new/@tanstack/angular-query-devtools-experimental@8299

@tanstack/angular-query-experimental

pnpm add https://pkg.pr.new/@tanstack/angular-query-experimental@8299

@tanstack/query-async-storage-persister

pnpm add https://pkg.pr.new/@tanstack/query-async-storage-persister@8299

@tanstack/query-broadcast-client-experimental

pnpm add https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@8299

@tanstack/eslint-plugin-query

pnpm add https://pkg.pr.new/@tanstack/eslint-plugin-query@8299

@tanstack/query-core

pnpm add https://pkg.pr.new/@tanstack/query-core@8299

@tanstack/query-devtools

pnpm add https://pkg.pr.new/@tanstack/query-devtools@8299

@tanstack/query-persist-client-core

pnpm add https://pkg.pr.new/@tanstack/query-persist-client-core@8299

@tanstack/query-sync-storage-persister

pnpm add https://pkg.pr.new/@tanstack/query-sync-storage-persister@8299

@tanstack/react-query

pnpm add https://pkg.pr.new/@tanstack/react-query@8299

@tanstack/react-query-devtools

pnpm add https://pkg.pr.new/@tanstack/react-query-devtools@8299

@tanstack/react-query-next-experimental

pnpm add https://pkg.pr.new/@tanstack/react-query-next-experimental@8299

@tanstack/react-query-persist-client

pnpm add https://pkg.pr.new/@tanstack/react-query-persist-client@8299

@tanstack/solid-query

pnpm add https://pkg.pr.new/@tanstack/solid-query@8299

@tanstack/solid-query-devtools

pnpm add https://pkg.pr.new/@tanstack/solid-query-devtools@8299

@tanstack/solid-query-persist-client

pnpm add https://pkg.pr.new/@tanstack/solid-query-persist-client@8299

@tanstack/svelte-query-devtools

pnpm add https://pkg.pr.new/@tanstack/svelte-query-devtools@8299

@tanstack/svelte-query

pnpm add https://pkg.pr.new/@tanstack/svelte-query@8299

@tanstack/svelte-query-persist-client

pnpm add https://pkg.pr.new/@tanstack/svelte-query-persist-client@8299

@tanstack/vue-query

pnpm add https://pkg.pr.new/@tanstack/vue-query@8299

@tanstack/vue-query-devtools

pnpm add https://pkg.pr.new/@tanstack/vue-query-devtools@8299

commit: 9a6220d

Copy link

codecov bot commented Nov 19, 2024

Codecov Report

Attention: Patch coverage is 50.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 62.69%. Comparing base (fadfbde) to head (9a6220d).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##           main    #8299       +/-   ##
=========================================
+ Coverage      0   62.69%   +62.69%     
=========================================
  Files         0      135      +135     
  Lines         0     4798     +4798     
  Branches      0     1351     +1351     
=========================================
+ Hits          0     3008     +3008     
- Misses        0     1548     +1548     
- Partials      0      242      +242     
Components Coverage Δ
@tanstack/angular-query-devtools-experimental ∅ <ø> (∅)
@tanstack/angular-query-experimental 88.25% <100.00%> (∅)
@tanstack/eslint-plugin-query ∅ <ø> (∅)
@tanstack/query-async-storage-persister 43.85% <100.00%> (∅)
@tanstack/query-broadcast-client-experimental ∅ <ø> (∅)
@tanstack/query-codemods ∅ <ø> (∅)
@tanstack/query-core 93.69% <100.00%> (∅)
@tanstack/query-devtools 4.78% <ø> (∅)
@tanstack/query-persist-client-core 57.73% <ø> (∅)
@tanstack/query-sync-storage-persister 84.61% <0.00%> (∅)
@tanstack/react-query 95.54% <ø> (∅)
@tanstack/react-query-devtools 10.00% <ø> (∅)
@tanstack/react-query-next-experimental ∅ <ø> (∅)
@tanstack/react-query-persist-client 100.00% <ø> (∅)
@tanstack/solid-query 78.20% <ø> (∅)
@tanstack/solid-query-devtools ∅ <ø> (∅)
@tanstack/solid-query-persist-client 100.00% <ø> (∅)
@tanstack/svelte-query 87.33% <0.00%> (∅)
@tanstack/svelte-query-devtools ∅ <ø> (∅)
@tanstack/svelte-query-persist-client 100.00% <ø> (∅)
@tanstack/vue-query 71.45% <ø> (∅)
@tanstack/vue-query-devtools ∅ <ø> (∅)
---- 🚨 Try these New Features:

@TkDodo TkDodo merged commit 7d36089 into TanStack:main Nov 19, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants