-
-
Notifications
You must be signed in to change notification settings - Fork 17
Helper type to narrow route props #121
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Test this file by running: | ||
| // npx tsc --noEmit test/node/pattern-match.types.ts | ||
|
|
||
| import type { RoutePropsForPath } from '../../src/router.js'; | ||
|
|
||
| // Test utils | ||
|
|
||
| type isEqualsType<T, U> = T extends U ? U extends T ? true : false : false; | ||
| type isWeakEqualsType<T, U> = T extends U ? true : false; | ||
|
|
||
| // Type tests based on router-match.test.js cases | ||
|
|
||
| // Base route test | ||
| const test1: isEqualsType< | ||
| RoutePropsForPath<'/'> , | ||
| { params: {} } | ||
| > = true; | ||
|
|
||
| const test1_1: isEqualsType< | ||
| RoutePropsForPath<'/'> , | ||
| { arbitrary: {} } | ||
| > = false; | ||
|
|
||
| // Param route test | ||
| const test2: isEqualsType< | ||
| RoutePropsForPath<'/user/:id'> , | ||
| { params: { id: string }, id: string } | ||
| > = true; | ||
|
|
||
| const test2_weak: isWeakEqualsType< | ||
| RoutePropsForPath<'/user/:id'> , | ||
| { params: { id: string } } | ||
| > = true; | ||
|
|
||
| // Param rest segment test | ||
| const test3: isEqualsType< | ||
| RoutePropsForPath<'/user/*'> , | ||
| { params: {}, rest: string } | ||
| > = true; | ||
|
|
||
| const test3_1: isEqualsType< | ||
| RoutePropsForPath<'/*'> , | ||
| { params: {}, rest: string } | ||
| > = true; | ||
|
|
||
| const test3_2: isEqualsType< | ||
| RoutePropsForPath<'*'> , | ||
| { params: {}, rest: string } | ||
| > = true; | ||
|
|
||
| // Param route with rest segment test | ||
| const test4: isEqualsType< | ||
| RoutePropsForPath<'/user/:id/*'> , | ||
| { params: { id: string }, id: string, rest: string } | ||
| > = true; | ||
|
|
||
| // Optional param route test | ||
| const test5: isEqualsType< | ||
| RoutePropsForPath<'/user/:id?'> , | ||
| { params: { id?: string }, id?: string } | ||
| > = true; | ||
|
|
||
| // Optional rest param route "/:x*" test | ||
| const test6: isEqualsType< | ||
| RoutePropsForPath<'/user/:id*'> , | ||
| { params: { id?: string }, id?: string } | ||
| > = true; | ||
|
|
||
| // rest param should not be present | ||
| const test6_error: isEqualsType< | ||
| RoutePropsForPath<'/user/:id*'> , | ||
| { params: { id: string }, rest: string } | ||
| > = false; | ||
|
|
||
| // Rest param route "/:x+" test | ||
| const test7: isEqualsType< | ||
| RoutePropsForPath<'/user/:id+'> , | ||
| { params: { id: string }, id: string } | ||
| > = true; | ||
|
|
||
| // rest param should not be present | ||
| const test7_error: isEqualsType< | ||
| RoutePropsForPath<'/user/:id+'>, | ||
| { params: { id: string }, id: string, rest: string } | ||
| > = false; | ||
|
|
||
| // Handles leading/trailing slashes test | ||
| const test8: isEqualsType< | ||
| RoutePropsForPath<'/about-late/:seg1/:seg2/'> , | ||
| { params: { seg1: string; seg2: string }, seg1: string, seg2: string } | ||
| > = true; | ||
|
|
||
| // Multiple params test (from overwrite properties test) | ||
| const test9: isEqualsType< | ||
| RoutePropsForPath<'/:path/:query'> , | ||
| { params: { path: string; query: string }, path: string, query: string } | ||
| > = true; | ||
|
|
||
| // Empty route test | ||
| const test10: isEqualsType< | ||
| RoutePropsForPath<''> , | ||
| { params: {} } | ||
| > = true; |
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.
Uh oh!
There was an error while loading. Please reload this page.