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

fix(vue-query): ensure built-in types are not being unwrapped #5954

Merged
merged 3 commits into from
Sep 7, 2023

Conversation

Mini-ghost
Copy link
Contributor

Resolve the following error

This error was mentioned in PR #5935 (comment)

import { useQuery, UseQueryOptions } from https://github.com/TanStack/query/tree/master/packages/vue-query;
import { Ref, toRef } from https://github.com/vuejs/core;

type MaybeRef<T> = T | Ref<T>;
type MaybeRefOrGetter<T> = MaybeRef<T> | (() => T);

const fetchUser = (userId: Date) =>
  fetch(`/api/user/${userId.toISOString()}`).then(res => res.json());

type User = any;
export function useUser(
  userId: MaybeRefOrGetter<Date>,
  options?: UseQueryOptions<User, any, User, readonly [string, Ref<Date>]>
) {
  return useQuery({
    queryKey: ['user', toRef(userId)],
    queryFn({ queryKey }) {
      return fetchUser(queryKey[1]);
    },
    ...options,
  } satisfies typeof options);
}
example.ts:185:24 - error TS2345: Argument of type '{ toString: {}; toDateString: {}; toTimeString: {}; toLocaleString: {}; toLocaleDateString: {}; toLocaleTimeString: {}; valueOf: {}; getTime: {}; getFullYear: {}; getUTCFullYear: {}; getMonth: {}; ... 32 more ...; [Symbol.toPrimitive]: {}; }' is not assignable to parameter of type 'Date'.
  Types of property 'toString' are incompatible.
    Type '{}' is not assignable to type '() => string'.
      Type '{}' provides no match for the signature '(): string'.

In that PR, not avoiding unwrapping built-in types resulted in errors, such as Ref, occurring.

Before

截圖 2023-09-04 下午7 35 04

After

截圖 2023-09-04 下午7 36 39

@vercel
Copy link

vercel bot commented Sep 4, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
query ⬜️ Ignored (Inspect) Visit Preview Sep 4, 2023 1:14pm

@nx-cloud
Copy link

nx-cloud bot commented Sep 4, 2023

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 46051ed. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this branch


✅ Successfully ran 2 targets

Sent with 💌 from NxCloud.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Sep 4, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 30766a5:

Sandbox Source
@tanstack/query-example-solid-basic-typescript Configuration
@tanstack/query-example-svelte-basic Configuration
@tanstack/query-example-vue-basic Configuration

@codesandbox-ci
Copy link

codesandbox-ci bot commented Sep 4, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 46051ed:

Sandbox Source
@tanstack/query-example-react-basic-typescript Configuration
@tanstack/query-example-svelte-basic Configuration

@codecov-commenter
Copy link

Codecov Report

Patch has no changes to coverable lines.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the GitHub App Integration for your organization. Read more.

Files Changed Coverage
packages/vue-query/src/useBaseQuery.ts ø
packages/vue-query/src/useInfiniteQuery.ts ø
packages/vue-query/src/useQuery.ts ø

📢 Thoughts on this report? Let us know!.

@Mini-ghost Mini-ghost marked this pull request as draft September 4, 2023 12:09
@Mini-ghost Mini-ghost marked this pull request as ready for review September 4, 2023 13:15
@callumacrae
Copy link

callumacrae commented Sep 4, 2023

what about user-defined classes?

thanks for picking this up so quickly!

@Mini-ghost
Copy link
Contributor Author

Mini-ghost commented Sep 4, 2023

When dealing with user-defined classes, we can draw inspiration from Vue's handling method, as demonstrated here:

https://github.com/vuejs/core/blob/0e8bbe873e579f3d3a74c44af28f7df9e7a06978/packages/reactivity/src/ref.ts#L459-L472

A possible implementation might resemble the following:

declare module '@tanstack/vue-query' {
   export interface RefUnwrapBailTypes {
     UserDefinedClass: UserDefinedClass;
   }
}

However, it's important to note that @tanstack/query-core will convert the queryKey into a string using JSON.stringify to use it as a key for cache access. This approach might encounter issues with types like Map, Set, or user-defined classes that cannot be stringified.

Given this consideration, I'm not sure if queryKey should support such a wide range of types.

It would be great to get feedback from @DamianOsipiuk .

@DamianOsipiuk DamianOsipiuk merged commit 35a4dd7 into TanStack:main Sep 7, 2023
10 checks passed
@Mini-ghost Mini-ghost deleted the fix/deep-unwrap-ref-type branch September 10, 2023 03:00
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

Successfully merging this pull request may close these issues.

None yet

4 participants