Skip to content
Source
Sign inSign up
TanStack/query

Enabled exactOptionalPropertyTypes causes error when trying to use the optional selector #7641

Send to my agent
Closedvladcos(GitHub profile, opens in a new tab) opened this issue 1 comment

Describe the bug

Per article https://tkdodo.eu/blog/react-query-data-transformations#3-using-the-select-option I'm using the optional select() passed from outside. When typing it as described by @TkDodo here https://stackoverflow.com/a/75788655/255408 an error comes up (see repro)

Your minimal, reproducible example

https://www.typescriptlang.org/play/?exactOptionalPropertyTypes=true#code/JYWwDg9gTgLgBAbwFBzgVwM4FMCKatQCeANEgL5wBmUEIcA5AAIwCGAdhqwMYDWA9FCwsuMALQBHfEXpIkMQmCxwAogA8CXYNgxwAvIjKysqyLDhcIHeJiwBxLDDUatWDAHkA0nrgAeACoAIiys3k5QmtoAfAAU2AA2WCIA-ABccNEAJsEsaWERrgCUepFwgdlFuiXIqIIwaFBs6Nh4BITR1ahwkq0eWIRpANr0xs7a9AC6pJ1dUoQAYmxp0RUlA5MonQB027FYCSJwSYhw8YnwFGkIZAVTcNcA3OSPRibQ8BZWTXYOeS4YygAlAFuAHefxBEL6X5RXb7GCpdJZVi5dThP4rUoQlgYjq1eqNGwtIjtDaobpEXr9OBDEZosbrabk+aLdIYta3VCnES3B5PIA

Steps to reproduce

The combinations of the typings and enabled exactOptionalPropertyTypes cause an error because an explicit undefined is not allowed

Expected behavior

The typings should allow explicit undefined

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Any

Tanstack Query adapter

react-query

TanStack Query version

v5.36.1

TypeScript version

v5.5.2

Additional context

No response

  1. I'd probably just do this:

    export const useGetExercisesOK2 = <TData = Exercises>(options?: { select?: (data: Exercises) => TData }) => {
      return useQuery({
        queryKey: ['exercises'],
        queryFn: () => [],
        ...options
      });
    };

    TypeScript playground

  2. TkDodo closed this

  3. TkDodo added labelstypes

  4. TkDodo added labelshas workaround

Sign in to join this conversation.