Skip to content

fix(query-core) Fix queries argument's type definition in queriesObserver #9449

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

yanlin-duan
Copy link

queries argument is not typed consistently in many of the methods in queriesObserver class. The constructor typed it to be Array<QueryObserverOptions<any, any, any, any, any>> but in few other places it's Array<QueryObserverOptions> which means the default will kick in, making it Array<QueryObserverOptions<unknown, DefaultError, unknown, unknown, QueryKey>.

This caused issues when I try to interact with queryObserver:

const queryOptions: Array<QueryObserverOptions<Data, Error, SelectedData, Data, QueryKey>> = { } // some query options

// defaultedOptions typed as DefaultedQueryObserverOptions<unknown, Error, unknown, unknown, QueryKey>[]
const defaultedOptions = queryOptions.map(queryOption => {
        const defaultedOption = queryClient.defaultQueryOptions(queryOption)
        defaultedOption._optimisticResults = isRestoring
          ? 'isRestoring'
          : 'optimistic'
        return defaultedOption
})

// OK
const queriesObserver = new QueriesObserver<Array<QueryObserverResult<SelectedData, Error>>>(
      queryClient,
      defaultedOptions
)

// ERROR
// Argument of type 'DefaultedQueryObserverOptions<Data, Error, SelectedData, Data, QueryKey>[]' is not assignable to parameter of type 'QueryObserverOptions<unknown, Error, unknown, unknown, QueryKey, never>[]'.
//   Type 'DefaultedQueryObserverOptions<Data, Error, SelectedData, Data, QueryKey>' is not assignable to type 'QueryObserverOptions<unknown, Error, unknown, unknown, QueryKey, never>'.
//     Types of property 'enabled' are incompatible.
//       Type 'Enabled<Data, Error, Data, QueryKey> | undefined' is not assignable to type 'Enabled<unknown, Error, unknown, QueryKey> | undefined'.
//         Type '(query: Query<Data, Error, Data, QueryKey>) => boolean' is not assignable to type 'Enabled<unknown, Error, unknown, QueryKey> | undefined'.
//           Type '(query: Query<Data, Error, Data, QueryKey>) => boolean' is not assignable to type '(query: Query<unknown, Error, unknown, QueryKey>) => boolean'.
//             Types of parameters 'query' and 'query' are incompatible.
//               Type 'Query<unknown, Error, unknown, QueryKey>' is not assignable to type 'Query<Data, Error, Data, QueryKey>'.
//                 The types of 'options.queryFn' are incompatible between these types.
//                   Type 'unique symbol | QueryFunction<unknown, QueryKey, never> | undefined' is not assignable to type 'unique symbol | QueryFunction<Data, QueryKey, never> | undefined'.
//                     Type 'QueryFunction<unknown, QueryKey, never>' is not assignable to type 'unique symbol | QueryFunction<Data, QueryKey, never> | undefined'.
//                       Type 'QueryFunction<unknown, QueryKey, never>' is not assignable to type 'QueryFunction<Data, QueryKey, never>'.
//                         Type 'unknown' is not assignable to type 'Data'.
//                           'Data' could be instantiated with an arbitrary type which could be unrelated to 'unknown'.ts(2345)
queriesObserver.setQueries(defaultedOptions)

In the fix, I used the same type that the constructor takes in for queryOptions in setQueries and a few other places.

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

Successfully merging this pull request may close these issues.

1 participant