Skip to content
Source
Sign inSign up
TanStack/router

feat(router-generator): add isolatedDeclarations option #8833

Openguru-irl(GitHub profile, opens in a new tab)wants to merge 11 commits intomain fromrefs/pull/8833/headopened

🎯 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.ts file. 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), and routeTree itself is exported. So with isolatedDeclarations on, tsc 6.0 and 7.0 report TS9010 for every route const and for routeTree, even though the file starts with // @ts-nocheck.

Changes

  • Adds an isolatedDeclarations option to the generator, off by default. Turn it on in tsr.config.json or in the bundler plugin's options, for example tanstackRouter({ 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 disableTypes on, 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 as any), Vue root components (the one new fixture), and disableTypes, whose output must not change.
  • isolated-declarations.test.ts compiles the nested and lazy snapshots with isolatedDeclarations on. 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:types and pnpm test:unit from 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 isolatedDeclarations option to generate route trees compatible with TypeScript’s isolated declaration checks.
    • Routes without a corresponding route file receive a permissive type in this mode.
  • Tests

    • Added coverage for isolated declaration generation across nested, lazy-loaded, and layout-based routes.
    1. 🦋 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
      NameType
      @tanstack/router-generatorPatch
      @tanstack/router-cliPatch
      @tanstack/router-pluginPatch
      @tanstack/start-plugin-corePatch
      @tanstack/router-vite-pluginPatch
      @tanstack/react-start-rscPatch
      @tanstack/react-startPatch
      @tanstack/solid-startPatch
      @tanstack/vue-startPatch

      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

    2. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →

      No actionable comments were generated in the recent review.

      Recent review info
      Run configuration
      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.md
      • packages/router-generator/src/config.ts
      • packages/router-generator/src/generator.ts
      • packages/router-generator/src/utils.ts
      • packages/router-generator/tests/generator.test.ts
      • packages/router-generator/tests/generator/isolated-declarations-root-components/routeTree.isolated.snapshot.ts
      • packages/router-generator/tests/generator/isolated-declarations-root-components/routeTree.snapshot.ts
      • packages/router-generator/tests/generator/isolated-declarations-root-components/routes/__root.component.vue
      • packages/router-generator/tests/generator/isolated-declarations-root-components/routes/__root.tsx
      • packages/router-generator/tests/generator/isolated-declarations-root-components/routes/about.component.vue
      • packages/router-generator/tests/generator/isolated-declarations-root-components/routes/about.lazy.tsx
      • packages/router-generator/tests/generator/isolated-declarations-root-components/routes/about.tsx
      • packages/router-generator/tests/generator/isolated-declarations-root-components/routes/index.tsx
      • packages/router-generator/tests/generator/lazy-only-layout-and-index/routeTree.isolated.snapshot.ts
      • packages/router-generator/tests/generator/lazy/routeTree.isolated.snapshot.ts
      • packages/router-generator/tests/generator/nested/routeTree.isolated.snapshot.ts
      • packages/router-generator/tests/isolated-declarations.test.ts
      • packages/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 isolatedDeclarations setting. 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

      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.ts
      The configuration adds isolatedDeclarations, defaulting to false. When enabled and types are active, the generator passes the option through recursive route-tree configuration and annotates virtual route imports with any.
      Typed route-tree output
      packages/router-generator/src/generator.ts, packages/router-generator/src/utils.ts
      Route updates and route-tree declarations receive explicit types in isolated mode. Routes with children use any for virtual routes and _addFileChildren return 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.json
      Generation 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.
      Priority: Normal

      Estimated 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 declarations

      Suggested labels: package: router-generator

      Suggested reviewers: schiller-manuel, birkskyum

      Merge Risk: Minimal · up to 99fd7

      No confirmed issue prevents merging after normal checks.

      Pre-merge checks: Passed 4 · Failed 1
      Failed Failed checks: 1 warning
      Check nameStatusExplanationResolution
      Docstring CoverageWarningDocstring 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 Passed checks: 4 passed
      Check nameStatusExplanation
      Title checkPassedThe 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 checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck skipped because no linked issues were found for this pull request.
      Description checkPassedThe 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.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.

Sign in to join this conversation.