-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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.tsfile are used internally to compute more preciseparamstypes for each route. Specifically, each route'sparamsare a union of all possibleparamsfrom any pages (visitable URLs) that include that route.For example:
Previously, we calculated the types for
routes/routeto be{ p: string, r: string }, but this is incorrect as it ignores possible params from child routes.Let's consider the possible
paramstypes 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
paramstype is a union of all theparamstypes 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?: undefinedentries for any fields not present in the current member but present in other members:This new type is equivalent to the unnormalized
Paramstype, 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. Powershreftypes.+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+registerand+virtualfrom existing projects?