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

Typescript support #23

Closed
lukaszfiszer opened this issue Nov 8, 2019 · 25 comments
Closed

Typescript support #23

lukaszfiszer opened this issue Nov 8, 2019 · 25 comments

Comments

@lukaszfiszer
Copy link

First of all - thanks for great library. It simplified data fetching in my apps by order of magnitude.

The biggest feature I'm missing is Typescript support - to the point I have manually added TS definitions to most part of the API by hand. @tannerlinsley would you be interested in having them added to definitely-typed? AFAIK this is a recommended way of adding typings to projects that are not written in TS.

@tannerlinsley
Copy link
Collaborator

tannerlinsley commented Nov 8, 2019 via email

@jparr721
Copy link

jparr721 commented Nov 9, 2019

@lukaszfiszer in the meantime, would you be willing to share your types file? I'd like to use this in my typescript project.

@lukaszfiszer
Copy link
Author

@rostero1
Copy link

I tried converting the codebase to Typescript, but had to quit an hour in. I've also never used Typescript before. I gave up when I realized the Data type was different for paginated results.

It doesn't compile, is only halfway done, and I probably introduced errors, but I'll leave it here for anyone that's interested.

https://gist.github.com/rostero1/4ad7088d445730a03cfdbf8475d3ea26

@tannerlinsley
Copy link
Collaborator

tannerlinsley commented Nov 10, 2019 via email

@lukaszfiszer
Copy link
Author

PR created in definitely-typed: DefinitelyTyped/DefinitelyTyped#40312

If anyone want to test those definitions locally in their project that would be a great help!

@lukaszfiszer
Copy link
Author

lukaszfiszer commented Nov 11, 2019

@tannerlinsley Thanks for review in dt repo.

However I've discovered there is an issue with types of useQuery. The data in QueryResult should be an array of resolved values when options.paginated=true. This is very difficult or even impossible to represent in Typescript - types cannot depend on runtime values.

For me it looks like that there are two versions of API of useQuery. Did you ever consider splitting it into the separate methods? Kind of like prefetchQuery is separated, even though it shares most of the options.

@tannerlinsley
Copy link
Collaborator

tannerlinsley commented Nov 12, 2019 via email

@siebertm
Copy link

However I've discovered there is an issue with types of useQuery. The data in QueryResult should be an array of resolved values when options.paginated=true. This is very difficult or even impossible to represent in Typescript - types cannot depend on runtime values.

I think this should be doable by defining a method overload on the type, like this:

interface QueryOptionsWithPagination extends QueryOptions {
  paginate: true
}

export function useQuery<TResult, TVariables extends object>(
    queryKey: QueryKey<TVariables>,
    queryFn: QueryFunction<TResult, TVariables>,
    options?: QueryOptionsWithPagination<TResult>
): QueryResult<TResult[], TVariables>;

I did not try it out, but that should do the trick.

@siebertm
Copy link

But, why can't the user just annotate with the proper type?

async function fetchPosts(): Promise<Post[]> {
  // ...
}

const { data, fetchMore } = useQuery<Post[]>('posts', fetchPosts, { paginated: true })

which can even be reduced due to TS type inferrence:

async function fetchPosts(): Promise<Post[]> {
  // ...
}

const { data, fetchMore } = useQuery('posts', fetchPosts, { paginated: true })

now data is of type Post[] | null and everything is fine.

I'd see it as a non-use case to have the same Query either paginated or not in the same component, wdyt?

@siebertm
Copy link

BTW: I tried some basic use cases in out TS code, it just works! Great work!

@tannerlinsley
Copy link
Collaborator

Types are now available via @types/react-query. For convenience, they are a dependency of this library by default.

@lukaszfiszer
Copy link
Author

@siebertm you right, definition overloading does the trick. I've submitted a PR to types package: DefinitelyTyped/DefinitelyTyped#40399

@bgazzera
Copy link

Hi @lukaszfiszer one question, shouldn't all the properties be optional?

export interface QueryOptions<TResult> {
    manual?: boolean;
    retry?: boolean | number;
    retryDelay?: (retryAttempt: number) => number;
    staleTime?: number;
    cacheTime?: number;
    refetchInterval?: false | number;
    onError?: (err: any) => void;
    onSucess?: (data: TResult) => void;
    suspense?: boolean;
}

@lukaszfiszer
Copy link
Author

lukaszfiszer commented Nov 20, 2019

@bgazzera well they are: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react-query/index.d.ts#L26

Did you rather mean options passed to ReactQueryConfigProvider? If yes, the fix is on the way: DefinitelyTyped/DefinitelyTyped#40514

@bgazzera
Copy link

Thanks! Yes you are right 😅

@LucaDe
Copy link

LucaDe commented Mar 10, 2020

Hey @lukaszfiszer , thanks for adding types. Do you have any plans to update them to match the newest react-query version? If not, I can support as well.

@julianwachholz
Copy link

@LucaDe type definitions for v1 are being worked on here: DefinitelyTyped/DefinitelyTyped#42830

@LucaDe
Copy link

LucaDe commented Mar 10, 2020

my bad, didn't see it. Awesome!!

@JeremiAnastaziak
Copy link

@tannerlinsley seems like typings in DefinitelyTyped are outdated, could you please take care about it?

@tannerlinsley
Copy link
Collaborator

@JeremiAnastaziak. I'm sorry they're out of date! Unfortunately, that's not my responsibility right now and is one of the reasons that they are 3rd party.

@JeremiAnastaziak
Copy link

sure, if u ever plan to add official support, I would love to help

@ghost
Copy link

ghost commented Mar 26, 2020

If the examples provided within the main repo could demonstrate some robust typescript examples that would be fantastic

@tannerlinsley
Copy link
Collaborator

PRs are open!

@tannerlinsley
Copy link
Collaborator

FYI - #362

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

9 participants