Repository navigation
fix: type useQuery's data from select, as TanStack does - #2
Merged
Merged
Conversation
The generated `useQuery` took `queryOptions?: Omit<UseQueryOptions, …>`. `UseQueryOptions` is a `MaybeRef<…>`, and `Omit` over that union keeps no key at all, so the options were neither typed nor tied to the endpoint: `select` received an implicit `any`, and `data` stayed typed as the raw payload whatever `select` returned. `useQuery` is now generic in `TData`, defaulting to the endpoint's data, and its options are the plain object variant of `UseQueryOptions<_QD, DefaultError, TData, _QD, QueryKey>`. TanStack infers `TData` from `select` as its own `useQuery` does, and a `select` expecting another input than the payload is refused. `fetchQuery` and `ensureQueryData` get their own option types, on the endpoint's data, without `select`. Every GET method now goes through one `_get` fetch, whose resolved type is the data type, with or without an output schema. The playground pins these types, and CI now runs `test:types`, which used to run only locally. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Node 20 lacks Object.groupBy, which eslint-flat-config-utils calls: lint has failed on main since July. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The root route (/api/) has no segment, so its key was emitted with a hole ([, ...query]) and, without a query, was [], the placeholder every disabled query shares. It now starts with an empty string. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The generated
useQuerytyped its options asOmit<UseQueryOptions, 'queryKey' | 'queryFn'>.UseQueryOptionsis aMaybeRef<…>, andOmitover that union keeps no key, so:selectreceived an implicitany;datastayed typed as the raw payload, whateverselectreturned.Change
useQueryis generic inTData(default: the endpoint's data). Its options are the plain variant ofUseQueryOptions<_QD, DefaultError, TData, _QD, QueryKey>, so TanStack infersTDatafromselectexactly like its ownuseQuery. Aselectexpecting another input than the payload is refused.fetchQuery/ensureQueryDatagetFetchQueryOptions/EnsureQueryDataOptionson the endpoint's data (noselect, as neither applies one)._getfetch;_QDis its resolved type, with or without an output schema.enabledandqueryKeybehave as before.Tests
playground/app.vuepins the types (checked bynpm run test:types):select→datais the payload, notany;select→datais the selected type, notany;selectwith a wrong input type → error;Against the previous generator, these checks fail with 4 errors. CI now runs
test:types, which previously ran only locally.Verified on a consumer app (Mesalia booking): replacing a hand-rolled TanStack query by the generated
useQuery(o, { select })typechecks with the selected types.🤖 Generated with Claude Code