Skip to content
Source
Sign inSign up
TanStack/query

Incorrect types for focusManager.setEventListener #6382

Send to my agent
ClosedBreakBB(GitHub profile, opens in a new tab) opened this issue 2 comments

Describe the bug

The method focusManager.setEventListener accepts a SetupFn as parameter. And a setupFn is a function with a parameter setFocused: (focused?: boolean) => void.

The docs provide the following example:

focusManager.setEventListener((handleFocus) => {
  // Listen to visibilitychange
  if (typeof window !== 'undefined' && window.addEventListener) {
    window.addEventListener('visibilitychange', handleFocus, false)
    return () => {
      // Be sure to unsubscribe if a new handler is set
      window.removeEventListener('visibilitychange', handleFocus)
    }
  }
})

While this code works (!), it is not valid in TypeScript, because handleFocus is actually setFocused: (focused?: boolean) => void which is not valid as event listener callback function.

Note: This is also an issue, when not explicitly typing handleFocus (as in the docs example).

Your minimal, reproducible example

here

Steps to reproduce

Check the call of window.addEventListener("visibilitychange", setFocused, false); towards TypeScript errors.

Expected behavior

The given example should be valid TypeScript.

How often does this bug happen?

Every time

Screenshots or Videos

Screenshot 2023-11-15 at 16 07 28

Platform

  • OS: macOS
  • Browser: Firefox, Chrome

Tanstack Query adapter

None

TanStack Query version

5.8.4

TypeScript version

6.9.1

Additional context

No response

  1. seems just like a docs issue? The actual code we use is:

    https://github.com/TanStack/query/blob/1d3ee672d2a1536e3463243813c91902aa97d973/packages/query-core/src/focusManager.ts#L16-L30

    so this indirection is all that's missing:

    const listener = () => setFocused()

    want to PR a docs change?

  2. TkDodo added labelsdocumentation

  3. want to PR a docs change?

    Sure!

  4. TkDodo closed this

Sign in to join this conversation.