Repository navigation
feat(pages): type useParams() from the page's URL pattern - #685
Draft
magurotuna wants to merge 3 commits into
Draft
magurotuna wants to merge 3 commits into
magurotuna wants to merge 3 commits into
Conversation
`useParams('/users/:id')` now returns `{ id: string }` in all four
adapters. The pattern must be one of the app's pages, so a misspelled
param or page is a type error.
- The generated `.void/routes.d.ts` adds `PagePathMap` (page patterns
and their param names, without HTTP or WebSocket routes), plus
`PagePath` and `PageParams<P>`. `void/routes` declares the same types
for apps without generated declarations.
- An unnamed catch-all (`[...]`) has no param key, matching
`c.req.param()`.
- At runtime, `useParams(pattern)` throws if the current page has no
value for one of the pattern's params, so a component rendered in a
different page doesn't read `undefined` typed as `string`. The check
lives in `assertPageParams()` in `void/pages-client`.
- `useParams()` and `useParams<T>()` keep their current types.
…tern union
A pattern typed as '/users/:id' | '/files/:path{.+}' returned
{ id: string; path: string }, promising a param the current page may
not have. PageParams now distributes over the union.
Collaborator
|
Note that this PR adds runtime checks to I'm not sure if we want to do runtime checks within If we're ok with just typechecking, consider this instead: useParams<'/posts/:id'>();or use your newly-added useParams<PageParams<'/posts/:id'>>();Since |
Member
|
I also prefer doing typechecking only and not failing during runtime for this. |
Member
Author
|
That makes sense, thanks for the insight! I'll make this static-check only |
This branch has not been deployed
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.
Pass a page's URL pattern to
useParams()to type its params.useParams()anduseParams<T>()are unchanged, and no config flag is needed.useParams(pattern)throws if the current page has no value for one of the pattern's params, instead of returningundefinedtyped asstring.void/routesexportsPagePathMap,PagePathandPageParams<P>. Both the generated.void/routes.d.tsand the package declare them.