Skip to content

Incorrect types for focusManager.setEventListener #6382

@BreakBB

Description

@BreakBB

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions