Skip to content

[Vue Query] queryOption type inference broken when using computed queryOption with select option #9789

@xpowerHZ

Description

@xpowerHZ

Describe the bug

queryOption type inference broken when using computed queryOption with select option

Your minimal, reproducible example

Sorry I couldn't find an existed vue example with tanstack query v5 set up to edit upon. But the steps should be really simple to reproduce

Steps to reproduce

1. Create a dynamic queryOption

const postsByUserIdQueryOption = (userId: string) => queryOption(...)

2. Use the computed queryOption in the component

const { data: posts } = useQuery(computed(() => postsByUserIdQueryOption(userId.value)))

3. Spread the options and add an enabled option - it works ✅

const { data: posts } = useQuery(computed(() => ({
  ...postsByUserIdQueryOption(userId.value),
  enabled: !!userId.value
})))

4. Add a select option - type error occurs ❌

const { data: postCount } = useQuery(computed(() => ({
  ...postsByUserIdQueryOption(userId.value),
  enabled: !!userId.value,
  select: (data) => data.length  // Monstrous typescript error
})))

but this ↓ non-dynamic queryOption without computed will work and postCount is correctly inferred to be a number ✅

const { data: postCount } = useQuery({
  ...postsByUserIdQueryOption(userId.value),
  enabled: !!userId.value,
  select: (data) => data.length  
})

5. Manually type data - fixes the type error ✅

const { data: postCount } = useQuery(computed(() => ({
  ...postsByUserIdQueryOption(userId.value),
  enabled: !!userId.value,
  select: (data: Post[]) => data.length
})))

Here ↓, it will also not infer correctly

const queryOption = computed(() => ({
    ...getPostsByUserIdQueryOption(userId.value)
    enabled: !!userId.value,
    select: (data) => data.length // typescript error here, data can be any, instead of the return type of queryFn defined inside queryOption
}))

Expected behavior

when having something like this

const { data: postCount } = useQuery(computed(() => ({
  ...postsByUserIdQueryOption(userId.value),
  enabled: !!userId.value,
  select: (data) => data.length
})))

the data parameter in select callback should be automatically inferred to the same type as the return type of queryFn provided in queryOption

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

  • vs code
  • vue query v5

Tanstack Query adapter

None

TanStack Query version

v5.83.0

TypeScript version

5.4.5

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions