-
-
Notifications
You must be signed in to change notification settings - Fork 10.7k
feat: better types for params
#13543
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
Conversation
🦋 Changeset detectedLatest commit: d152143 The changes in this PR will be included in the next version bump. This PR includes changesets to release 11 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
656a9a3
to
ae73463
Compare
🤖 Hello there, We just published version Thanks! |
The types registered by the new generated
+routes-pre.ts
file are used internally to compute more preciseparams
types for each route. Specifically, each route'sparams
are a union of all possibleparams
from any pages (visitable URLs) that include that route.For example:
Previously, we calculated the types for
routes/route
to be{ p: string, r: string }
, but this is incorrect as it ignores possible params from child routes.Let's consider the possible
params
types forroutes/route
:routes/route
?parent/1
{ p: string }
parent/1/route/2
{ p: string, r: string }
parent/1/route/2/child1/3/4
{ p: string, r: string, c1a: string, c1b: string }
parent/1/route/2/child2/5/6
{ p: string, r: string, c2a: string, c2b: string }
So the correct
params
type is a union of all theparams
types for pages that includesroutes/route
:We could stop here, but this will lead to poor DX as autocompletion for
params.
will only suggest fields that exist in all members of the union. To fix this, we normalize the members of the union and add?: undefined
entries for any fields not present in the current member but present in other members:This new type is equivalent to the unnormalized
Params
type, but allows for better DX from autocompletions.This PR also reorganizes the generated files for types as:
+future.ts
: Automatic registration of future flag types+pages.ts
: Types relevant to each visitable URL includingparams
. Powershref
types.+routes-pre.ts
: Types derived from routes config, prior to seeing any route modules+routes-post.ts
: Coming soon! Not part of this PR, but this will allow us to register module-aware types for routes!+server-build.d.ts
: Types forvirtual:react-router/server-build
(previously+virtual.d.ts
)TODO
todo.md
+ playground changesNormalize
+register
and+virtual
from existing projects?