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

[atomsWithQuery]: queryKey type is always unknown #25

Open
amirhhashemi opened this issue Mar 3, 2023 · 4 comments
Open

[atomsWithQuery]: queryKey type is always unknown #25

amirhhashemi opened this issue Mar 3, 2023 · 4 comments

Comments

@amirhhashemi
Copy link

amirhhashemi commented Mar 3, 2023

tanstack-query infers the type of queryKey parameter of queryFn based on the queryKey option of useQuery:

useQuery({
    queryKey: ["x", "y"],
    queryFn: async ({queryKey: [x, y]}) => {
        // typeof x = string | undefined
        // typeof y = string | undefined
    },
})

But it's Jotai equivalent doesn't:

atomsWithQuery(() => ({
    queryKey: ["x", "y"],
    queryFn: async ({queryKey: [x, y]}) => {
        // typeof x = unknown
        // typeof y = unknown
    },
}))
@amirhhashemi amirhhashemi changed the title [useAtomsWithQuery]: queryKey is not typed correctly! [atomsWithQuery]: queryKey is not typed correctly! Mar 3, 2023
@amirhhashemi amirhhashemi changed the title [atomsWithQuery]: queryKey is not typed correctly! [atomsWithQuery]: queryKey is not typed correctly?! Mar 3, 2023
@amirhhashemi amirhhashemi changed the title [atomsWithQuery]: queryKey is not typed correctly?! [atomsWithQuery]: queryKey type is always unknown Mar 3, 2023
@dai-shi
Copy link
Member

dai-shi commented Mar 3, 2023

Nice catch. Do you know how to fix?

@amirhhashemi
Copy link
Author

Nice catch. Do you know how to fix?

I briefly looked at the source code but couldn't find the problem. But I think I can fix it. You can expect a PR today.

@amirhhashemi
Copy link
Author

amirhhashemi commented Mar 3, 2023

Turned out that It's a Typescript bug. This works:

atomsWithQuery(() => ({
    queryKey: ["x", "y"],
    queryFn: async ({queryKey: [x, y]}) => {
        // typeof x = string
        // typeof y = string
    },
}))

But this doesn't work because I used the get function that somehow messes with Typescript's type inference:

atomsWithQuery((get) => ({
    queryKey: ["x", "y"],
    queryFn: async ({queryKey: [x, y]}) => {
        // typeof x = unknown
        // typeof y = unknown
    },
}))

It's potentioally related to this issue: microsoft/TypeScript#47599
Also this Stackoverflow provides more information: https://stackoverflow.com/questions/75629273/typescript-falls-back-to-the-default-value-of-generic-type-for-no-reason

AFAIK, there is no fix for that yet.

@amirhhashemi
Copy link
Author

amirhhashemi commented Mar 4, 2023

The workaround is to explicitly annotate get (more info):

import type { Getter } from "jotai"

atomsWithQuery((get: Getter) => ({
    queryKey: ["x", "y"],
    queryFn: async ({queryKey: [x, y]}) => {
        // typeof x = string
        // typeof y = string
    },
}))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants