-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add Route.createLink() for type-safe relative links #6367
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
base: main
Are you sure you want to change the base?
Conversation
Adds optional `{ from }` config to `createLink` for fixing the origin path.
Enables type-safe relative navigation (e.g., `./child`, `../sibling`) from a specific route.
📝 WalkthroughWalkthroughAdds a new CreateLinkOptions type with a required Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/react-router/tests/createLink-from.test-d.tsx (1)
1-1: Remove unused importexpectTypeOf.The
expectTypeOffunction is imported but never used in this file. The tests rely on@ts-expect-errorannotations and standalone JSX expressions instead.Suggested fix
-import { expectTypeOf, test } from 'vitest' +import { test } from 'vitest'
📜 Review details
Configuration used: defaults
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/react-router/src/link.tsxpackages/react-router/tests/createLink-from.test-d.tsx
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
Use TypeScript strict mode with extensive type safety for all code
Files:
packages/react-router/src/link.tsxpackages/react-router/tests/createLink-from.test-d.tsx
**/*.{js,ts,tsx}
📄 CodeRabbit inference engine (AGENTS.md)
Implement ESLint rules for router best practices using the ESLint plugin router
Files:
packages/react-router/src/link.tsxpackages/react-router/tests/createLink-from.test-d.tsx
🧠 Learnings (4)
📚 Learning: 2025-10-08T08:11:47.088Z
Learnt from: nlynzaad
Repo: TanStack/router PR: 5402
File: packages/router-generator/tests/generator/no-formatted-route-tree/routeTree.nonnested.snapshot.ts:19-21
Timestamp: 2025-10-08T08:11:47.088Z
Learning: Test snapshot files in the router-generator tests directory (e.g., files matching the pattern `packages/router-generator/tests/generator/**/routeTree*.snapshot.ts` or `routeTree*.snapshot.js`) should not be modified or have issues flagged, as they are fixtures used to verify the generator's output and are intentionally preserved as-is.
Applied to files:
packages/react-router/tests/createLink-from.test-d.tsx
📚 Learning: 2025-12-06T15:03:07.223Z
Learnt from: CR
Repo: TanStack/router PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-12-06T15:03:07.223Z
Learning: Applies to **/*.{js,ts,tsx} : Implement ESLint rules for router best practices using the ESLint plugin router
Applied to files:
packages/react-router/tests/createLink-from.test-d.tsx
📚 Learning: 2025-12-17T02:17:55.086Z
Learnt from: schiller-manuel
Repo: TanStack/router PR: 6120
File: packages/router-generator/src/generator.ts:654-657
Timestamp: 2025-12-17T02:17:55.086Z
Learning: In `packages/router-generator/src/generator.ts`, pathless_layout routes must receive a `path` property when they have a `cleanedPath`, even though they are non-path routes. This is necessary because child routes inherit the path from their parent, and without this property, child routes would not have the correct full path at runtime.
Applied to files:
packages/react-router/tests/createLink-from.test-d.tsx
📚 Learning: 2025-10-14T18:59:33.990Z
Learnt from: FatahChan
Repo: TanStack/router PR: 5475
File: e2e/react-start/basic-prerendering/src/routes/redirect/$target/via-beforeLoad.tsx:8-0
Timestamp: 2025-10-14T18:59:33.990Z
Learning: In TanStack Router e2e test files, when a route parameter is validated at the route level (e.g., using zod in validateSearch or param validation), switch statements on that parameter do not require a default case, as the validation ensures only expected values will reach the switch.
Applied to files:
packages/react-router/tests/createLink-from.test-d.tsx
🧬 Code graph analysis (2)
packages/react-router/src/link.tsx (3)
packages/router-core/src/index.ts (4)
AnyRouter(208-208)RoutePaths(55-55)RegisteredRouter(210-210)Constrain(306-306)packages/router-core/src/router.ts (2)
AnyRouter(787-787)RegisteredRouter(117-121)packages/router-core/src/utils.ts (1)
Constrain(131-133)
packages/react-router/tests/createLink-from.test-d.tsx (1)
packages/virtual-file-routes/src/api.ts (1)
rootRoute(10-19)
🔇 Additional comments (6)
packages/react-router/tests/createLink-from.test-d.tsx (2)
18-65: LGTM!The route tree setup is well-structured, matching the documented hierarchy. The module augmentation correctly registers the router type for type inference in tests.
72-108: Comprehensive type coverage for the newfromoption.The test cases effectively validate:
- Valid relative paths (
./settings,./users)- Absolute paths (
/posts)- Parent navigation (
..)- Invalid relative paths with proper
@ts-expect-errorannotations- Required params enforcement for parameterized routes
packages/react-router/src/link.tsx (4)
550-555: LGTM!The
CreateLinkOptionsinterface correctly constrainsTFromto valid route paths. The requiredfromfield ensures type-safe relative navigation is properly configured when options are provided.
574-583: Well-designed generic signature for type inference.The
constmodifiers onTCompandTFromensure literal type inference. When callingcreateLink(Button, { from: '/dashboard' }), TypeScript correctly infers:
TCompastypeof ButtonTFromas the literal'/dashboard'The return type
LinkComponent<TComp, TFrom>properly propagates the fixedfrompath as the default for the component's navigation props.
584-588: LGTM!The implementation correctly forwards the
fromvalue using optional chaining (options?.from), which gracefully handles the case whenoptionsis undefined by falling back toLink's default context-based behavior.
566-572: Good documentation updates.The JSDoc clearly documents the new
optionsparameter and provides practical examples showing both basiccreateLinkusage and the newfromoption for type-safe relative navigation.
- RouteApi.createLink(Comp) for getRouteApi usage - Route.createLink(Comp) for createFileRoute/createRoute usage - Updated tests to show both syntaxes work (with and without ./ prefix)
from option to createLink for type-safe relative navigation
Summary
Route.createLink(Comp)method for type-safe relative navigation from a route{ from }option to standalonecreateLink()for the same functionalityUsage
Test plan