Skip to content

πŸ› Type inference bug when calling createQuery β€” TQueryFnData not preserved correctlyΒ #15

@Tinkerbells

Description

@Tinkerbells

Library version 5.3.0


🧠 Summary

Calling createQuery with explicit generics doesn't preserve the TQueryFnData type correctly. This results in a TypeScript type mismatch, particularly around _result, because the function returns a type where TQueryFnData remains unresolved.


βœ… Expected Behavior

The returned query should have the correct TQueryFnData, matching the one explicitly passed during the call.


❌ Actual Behavior

Instead of resolving TQueryFnData to FsrsCardWithContent[], the returned type keeps it generic, leading to this mismatch:

Type 'Query<FsrsCardWithContent[], NetError, TQueryFnData, TQueryFnData, readonly unknown[]>' is not assignable to type 'Query<FsrsCardWithContent[], NetError, FsrsCardWithContent[], FsrsCardWithContent[], readonly unknown[]>'

πŸ“¦ Code Sample

public cacheCreateQuery<
  TQueryFnData = unknown,
  TError = DefaultError,
  TData = TQueryFnData,
  TQueryData = TQueryFnData,
  TQueryKey extends QueryKey = QueryKey,
>(
  queryFn: QueryFn<TQueryFnData, TError, TData, TQueryData, TQueryKey>,
  params?: CreateQueryParams<TQueryFnData, TError, TData, TQueryData, TQueryKey>,
) {
  return createQuery<TQueryFnData, TError, TData, TQueryData, TQueryKey>({
    queryFn,
    queryClient: this.queryClient,
    ...params,
  })
}

Usage:

private readonly dueCardsQuery: Query<FsrsCardWithContent[], NetError>
this.dueCardsQuery = this.cache.createQuery<FsrsCardWithContent[], NetError>(
  () => this.fsrsService.findDueByDeckId(this._deckId),
  {
    queryKey: this.keys.studyCards(this._deckId),
    onSuccess: (data) => this._initializeStudyState(data),
    onError: (error) => {
      this.notify.error(error.message)
      this.finishSession()
    },
    enableOnDemand: true,
    enabled: ({ queryKey }) => typeof queryKey[1] === 'string',
  },
)

πŸ§ͺ Diagnostics

Type 'Query<FsrsCardWithContent[], NetError, TQueryFnData, TQueryFnData, readonly unknown[]>' 
is not assignable to type 'Query<FsrsCardWithContent[], NetError, FsrsCardWithContent[], FsrsCardWithContent[], readonly unknown[]>'.

πŸ“ Reference: Custom CacheService Implementation

You can see the full CacheService implementation here:
πŸ”— https://github.com/Tinkerbells/dolphy-frontend/blob/feat/mvc/src/common/services/cache.ts

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions