-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(router-core): scope staticData typing by route-id prefix #8140
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
Open
striedinger
wants to merge
2
commits into
TanStack:main
Choose a base branch
from
striedinger:striedinger/static-data-by-route-prefix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
218 changes: 218 additions & 0 deletions
218
packages/react-router/tests/staticDataByRoutePrefix.test-d.tsx
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,218 @@ | ||
| import { expectTypeOf, test } from 'vitest' | ||
| import { createFileRoute, createRootRoute, createRoute } from '../src' | ||
| import type { | ||
| AnyRoute, | ||
| StaticDataByRouteId, | ||
| StaticDataRouteOption, | ||
| UpdatableStaticRouteOptionByRouteId, | ||
| } from '../src' | ||
|
|
||
| interface PillarStaticData { | ||
| layout: 'pillar' | ||
| collapsible?: boolean | ||
| } | ||
|
|
||
| interface DetailStaticData { | ||
| layout: 'detail' | ||
| backTo: string | ||
| } | ||
|
|
||
| interface NestedStaticData { | ||
| layout: 'nested' | ||
| depth: number | ||
| } | ||
|
|
||
| declare module '@tanstack/router-core' { | ||
| interface StaticDataByRoutePrefix { | ||
| '/_staticPillar': PillarStaticData | ||
| '/_staticDetail': DetailStaticData | ||
| '/_staticPillar/nested': NestedStaticData | ||
| } | ||
| } | ||
|
|
||
| const rootRoute = createRootRoute() | ||
|
|
||
| const pillarRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| id: '_staticPillar', | ||
| staticData: { layout: 'pillar' }, | ||
| }) | ||
|
|
||
| const pillarFileRoute = createFileRoute('/_staticPillar')() | ||
|
|
||
| const pillarFileChildRoute = createFileRoute('/_staticPillar/dashboard')({ | ||
| staticData: { layout: 'pillar', collapsible: true }, | ||
| }) | ||
|
|
||
| declare module '@tanstack/router-core' { | ||
| interface FileRoutesByPath { | ||
| '/_staticPillar': { | ||
| preLoaderRoute: typeof pillarFileRoute | ||
| parentRoute: typeof rootRoute | ||
| id: '/_staticPillar' | ||
| fullPath: string | ||
| path: string | ||
| } | ||
| '/_staticPillar/dashboard': { | ||
| preLoaderRoute: typeof pillarFileChildRoute | ||
| parentRoute: typeof pillarFileRoute | ||
| id: '/_staticPillar/dashboard' | ||
| fullPath: '/dashboard' | ||
| path: '/dashboard' | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test('StaticDataByRouteId maps a route id to its registered prefix shape', () => { | ||
| expectTypeOf< | ||
| StaticDataByRouteId<'/_staticPillar'> | ||
| >().toEqualTypeOf<PillarStaticData>() | ||
| expectTypeOf< | ||
| StaticDataByRouteId<'/_staticPillar/dashboard'> | ||
| >().toEqualTypeOf<PillarStaticData>() | ||
| expectTypeOf< | ||
| StaticDataByRouteId<'/_staticDetail/settings/profile'> | ||
| >().toEqualTypeOf<DetailStaticData>() | ||
| }) | ||
|
|
||
| test('StaticDataByRouteId only matches whole path segments', () => { | ||
| expectTypeOf< | ||
| StaticDataByRouteId<'/_staticPillarExtra'> | ||
| >().toEqualTypeOf<StaticDataRouteOption>() | ||
| }) | ||
|
|
||
| test('StaticDataByRouteId falls back to StaticDataRouteOption when no prefix matches', () => { | ||
| expectTypeOf< | ||
| StaticDataByRouteId<'/unrelated'> | ||
| >().toEqualTypeOf<StaticDataRouteOption>() | ||
| }) | ||
|
|
||
| test('overlapping registered prefixes union their shapes', () => { | ||
| expectTypeOf<StaticDataByRouteId<'/_staticPillar/nested'>>().toEqualTypeOf< | ||
| PillarStaticData | NestedStaticData | ||
| >() | ||
| expectTypeOf< | ||
| StaticDataByRouteId<'/_staticPillar/nested/leaf'> | ||
| >().toEqualTypeOf<PillarStaticData | NestedStaticData>() | ||
|
|
||
| createRoute({ | ||
| getParentRoute: () => pillarRoute, | ||
| path: 'nested/leaf', | ||
| staticData: { layout: 'nested', depth: 1 }, | ||
| }) | ||
|
|
||
| createRoute({ | ||
| getParentRoute: () => pillarRoute, | ||
| path: 'nested/leaf', | ||
| staticData: { layout: 'pillar' }, | ||
| }) | ||
|
|
||
| createRoute({ | ||
| getParentRoute: () => pillarRoute, | ||
| path: 'nested/leaf', | ||
| // @ts-expect-error neither registered shape allows DetailStaticData | ||
| staticData: { layout: 'detail', backTo: '/' }, | ||
| }) | ||
| }) | ||
|
|
||
| test('code-based routes type staticData by the registered prefix of their id', () => { | ||
| expectTypeOf(pillarRoute.id).toEqualTypeOf<'/_staticPillar'>() | ||
|
|
||
| const dashboardRoute = createRoute({ | ||
| getParentRoute: () => pillarRoute, | ||
| path: 'dashboard', | ||
| staticData: { layout: 'pillar', collapsible: true }, | ||
| }) | ||
|
|
||
| expectTypeOf(dashboardRoute.id).toEqualTypeOf<'/_staticPillar/dashboard'>() | ||
|
|
||
| createRoute({ | ||
| getParentRoute: () => pillarRoute, | ||
| path: 'reports', | ||
| // @ts-expect-error a route under `/_staticPillar` must use PillarStaticData | ||
| staticData: { layout: 'detail', backTo: '/' }, | ||
| }) | ||
|
|
||
| // presence stays optional under a prefix, even with required properties | ||
| createRoute({ | ||
| getParentRoute: () => pillarRoute, | ||
| path: 'metrics', | ||
| }) | ||
| }) | ||
|
|
||
| test('update() types staticData by the registered prefix of the route id', () => { | ||
| pillarRoute.update({ staticData: { layout: 'pillar' } }) | ||
|
|
||
| pillarRoute.update({ | ||
| // @ts-expect-error a route under `/_staticPillar` must use PillarStaticData | ||
| staticData: { layout: 'detail', backTo: '/' }, | ||
| }) | ||
| }) | ||
|
|
||
| test('update() keeps the StaticDataRouteOption behavior outside registered prefixes', () => { | ||
| const legalRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: 'legal', | ||
| }) | ||
|
|
||
| legalRoute.update({ staticData: { anything: 'goes' } }) | ||
| }) | ||
|
|
||
| test('a matching prefix replaces the staticData option instead of intersecting it', () => { | ||
| // exactly the registered shape, not `StaticDataRouteOption & <shape>` | ||
| expectTypeOf< | ||
| Parameters<typeof pillarRoute.update>[0]['staticData'] | ||
| >().toEqualTypeOf<PillarStaticData | undefined>() | ||
|
|
||
| const legalRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: 'legal', | ||
| }) | ||
|
|
||
| expectTypeOf< | ||
| Parameters<typeof legalRoute.update>[0]['staticData'] | ||
| >().toEqualTypeOf<StaticDataRouteOption | undefined>() | ||
|
|
||
| // That a prefix also lifts a *required* `StaticDataRouteOption` | ||
| // augmentation (the "Enforcing Static Data" pattern) cannot be asserted | ||
| // here: augmenting that interface would leak into every other test of | ||
| // this shared tsconfig project, so that case is pinned only by the | ||
| // implementation of `UpdatableStaticRouteOptionByRouteId`. | ||
| }) | ||
|
|
||
| test('file-based routes type staticData by the registered prefix of their id', () => { | ||
| expectTypeOf( | ||
| pillarFileChildRoute.id, | ||
| ).toEqualTypeOf<'/_staticPillar/dashboard'>() | ||
|
|
||
| createFileRoute('/_staticPillar/dashboard')({ | ||
| // @ts-expect-error a route under `/_staticPillar` must use PillarStaticData | ||
| staticData: { layout: 'detail', backTo: '/' }, | ||
| }) | ||
|
|
||
| // presence stays optional under a prefix, even with required properties | ||
| createFileRoute('/_staticPillar/dashboard')({}) | ||
| }) | ||
|
|
||
| test('routes outside any registered prefix keep the StaticDataRouteOption behavior', () => { | ||
| const aboutRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| path: 'about', | ||
| staticData: { anything: 'goes' }, | ||
| }) | ||
|
|
||
| expectTypeOf(aboutRoute.id).toEqualTypeOf<'/about'>() | ||
| }) | ||
|
|
||
| test('non-literal route ids type staticData permissively once a prefix is registered', () => { | ||
| // Wide instantiations such as `AnyRoute` cover prefixed routes (optional, | ||
| // prefix-shaped staticData) and unprefixed routes (StaticDataRouteOption) | ||
| // at once, so non-literal ids widen to `{ staticData?: any }`. | ||
| expectTypeOf<UpdatableStaticRouteOptionByRouteId<string>>().toEqualTypeOf<{ | ||
| staticData?: any | ||
| }>() | ||
|
|
||
| // ...which keeps prefixed and unprefixed routes assignable to AnyRoute. | ||
| const routes: Array<AnyRoute> = [rootRoute, pillarRoute, pillarFileChildRoute] | ||
| expectTypeOf(routes).toEqualTypeOf<Array<AnyRoute>>() | ||
| }) |
Oops, something went wrong.
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.