feat(router-generator): add isolatedDeclarations option #8833
main fromrefs/pull/8833/head·opened 🎯 Changes#
AI disclosure: an AI coding agent wrote the code, the tests and this description.
Motivation#
- With
isolatedDeclarations, declaration emit needs an explicit type on every declaration that ends up in a.d.tsfile. Tools such as oxc rely on this to emit declarations one file at a time. - The generated route tree has no annotations. Its exported interfaces refer to each route const (
typeof PostsRoute), androuteTreeitself is exported. So withisolatedDeclarationson,tsc6.0 and 7.0 report TS9010 for every route const and forrouteTree, even though the file starts with// @ts-nocheck.
Changes#
- Adds an
isolatedDeclarationsoption to the generator, off by default. Turn it on intsr.config.jsonor in the bundler plugin's options, for exampletanstackRouter({ isolatedDeclarations: true }). - With the option on, the generator gives each const in the route tree an explicit type annotation. Each annotation names the type TypeScript already infers, so the route types stay exact.
- The generator can't write an exact annotation for a virtual route, such as a lazy file without a route file, so it types that route as
any. Adding a route file for the path keeps its exact type. - With the option off, or with
disableTypeson, the generated output doesn't change. - The option covers only the generated route tree. Route files still need their own annotations.
Tests#
- The generator tests rerun five fixtures with the option on and compare each against a second snapshot,
routeTree.isolated.snapshot.ts: nested, lazy, lazy-only routes (typed asany), Vue root components (the one new fixture), anddisableTypes, whose output must not change. isolated-declarations.test.tscompiles the nested and lazy snapshots withisolatedDeclarationson. The annotated tree has no errors, the plain tree reports TS9010, and both trees have the same type.- Passing locally:
pnpm test:eslint,pnpm test:typesandpnpm test:unitfrom the repo root.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with the relevant test commands, or tests do not apply to this pull request.
- I fully understand the code in this pull request, including any code generated with AI assistance.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
New Features
- Added an
isolatedDeclarationsoption to generate route trees compatible with TypeScript’s isolated declaration checks. - Routes without a corresponding route file receive a permissive type in this mode.
- Added an
Tests
- Added coverage for isolated declaration generation across nested, lazy-loaded, and layout-based routes.
- 1
No actionable comments were generated in the recent review.
Recent review info-
Run configuration-
- Source mode: Auto
- Repository: TanStack/router
- Branch: main
- File: .coderabbit.yaml
- Commit: da2adec
- Layers: Repository YAML
- Review profile: CHILL
- Plan: Advanced
- Run ID:
a1eadc81-cfcf-4771-8791-0c4f3fb2121e
Commits-
Reviewing files that changed from the base of the PR and between da2adec76dbe6c0da6d6a0d5640f7b9388217f45 and 99fd70c2b0bde0f94bca14535004d27c89ace67f.
Files selected for processing: 18-
.changeset/router-generator-isolated-declarations.mdpackages/router-generator/src/config.tspackages/router-generator/src/generator.tspackages/router-generator/src/utils.tspackages/router-generator/tests/generator.test.tspackages/router-generator/tests/generator/isolated-declarations-root-components/routeTree.isolated.snapshot.tspackages/router-generator/tests/generator/isolated-declarations-root-components/routeTree.snapshot.tspackages/router-generator/tests/generator/isolated-declarations-root-components/routes/__root.component.vuepackages/router-generator/tests/generator/isolated-declarations-root-components/routes/__root.tsxpackages/router-generator/tests/generator/isolated-declarations-root-components/routes/about.component.vuepackages/router-generator/tests/generator/isolated-declarations-root-components/routes/about.lazy.tsxpackages/router-generator/tests/generator/isolated-declarations-root-components/routes/about.tsxpackages/router-generator/tests/generator/isolated-declarations-root-components/routes/index.tsxpackages/router-generator/tests/generator/lazy-only-layout-and-index/routeTree.isolated.snapshot.tspackages/router-generator/tests/generator/lazy/routeTree.isolated.snapshot.tspackages/router-generator/tests/generator/nested/routeTree.isolated.snapshot.tspackages/router-generator/tests/isolated-declarations.test.tspackages/router-generator/tsconfig.json
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
-
Walkthrough
-
The router generator adds an opt-in
isolatedDeclarationssetting. When enabled and types are not disabled, it emits explicitly typed route-tree declarations. New snapshots and TypeScript tests cover several route configurations.Changes: Isolated declaration generation
Priority:Layer / File(s) Summary Option and mode wiring
packages/router-generator/src/config.ts,packages/router-generator/src/generator.ts,packages/router-generator/src/utils.tsThe configuration adds isolatedDeclarations, defaulting tofalse. When enabled and types are active, the generator passes the option through recursive route-tree configuration and annotates virtual route imports withany.Typed route-tree output
packages/router-generator/src/generator.ts,packages/router-generator/src/utils.tsRoute updates and route-tree declarations receive explicit types in isolated mode. Routes with children use anyfor virtual routes and_addFileChildrenreturn types for other routes. Other configurations retain the existing output form.Generation and compilation coverage
.changeset/*,packages/router-generator/tests/generator.test.ts,packages/router-generator/tests/generator/*/routeTree*.snapshot.ts,packages/router-generator/tests/generator/isolated-declarations-root-components/*,packages/router-generator/tests/isolated-declarations.test.ts,packages/router-generator/tsconfig.jsonGeneration snapshots cover nested, lazy, types-disabled, and Vue route fixtures. Compilation tests check diagnostics and route-tree types. The package excludes isolated snapshots from compilation. NormalEstimated code review effort: 3 (Moderate) | ~20 minutes
Change: Feature
Sequence Diagram(s)#
sequenceDiagram participant Generator participant buildRouteTreeConfig participant routeTree Generator->>buildRouteTreeConfig: Pass isolatedDeclarations option buildRouteTreeConfig-->>Generator: Return route configuration Generator->>routeTree: Emit explicitly typed declarationsSuggested labels:
package: router-generatorSuggested reviewers:
schiller-manuel,birkskyum
Merge Risk:
Minimal · up to99fd7-
No confirmed issue prevents merging after normal checks.
Pre-merge checks:
4 · 1-
Failed checks: 1 warningCheck name Status Explanation Resolution Docstring Coverage Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… Write docstrings for the functions missing them to satisfy the coverage threshold. Passed checks: 4 passed-
Check name Status Explanation Title check The title "feat(router-generator): add isolatedDeclarations option" names the main change: a new isolatedDeclarations option in the router-generator. The changeset, config schema, generator, and utils… Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check skipped because no linked issues were found for this pull request. Description check The pull request description follows the required template. It explains the motivation and changes, discloses AI authorship, completes the checklist, documents testing, and identifies the release impa… Full details: Title check
-
Explanation
The title "feat(router-generator): add isolatedDeclarations option" names the main change: a new isolatedDeclarations option in the router-generator. The changeset, config schema, generator, and utils edits all implement that option. The title is specific, short, and follows conventional commit style.
Full details: Description check
-
Explanation
The pull request description follows the required template. It explains the motivation and changes, discloses AI authorship, completes the checklist, documents testing, and identifies the release impact with a changeset.
-
Full details: Docstring Coverage
-
Explanation
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 2 functions across 14 files. (4 skipped: 4 unsupported.)
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
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 `@coderabbitai help` to get the list of available commands.-
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: 99fd70c2b0bde0f94bca14535004d27c89ace67f
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 9 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