Getting Started
1. Install
One package provides both the build-time plugin (modernjs-typed-routes/plugin) and the runtime
wrappers (modernjs-typed-routes).
2. Register the plugin
3. Generate
Types are generated automatically when you run modern dev or modern build, and regenerate
whenever your routes change while the dev server runs — a route file is added, removed or renamed,
or modern.routes.ts is edited. (Editing a page's content doesn't regenerate anything: the
routes didn't change.) To generate without starting anything (e.g. CI):
You'll get a single generated file:
Because the file lives in src/, your existing tsconfig.json picks it up with no extra
configuration — and you can always open it to see exactly what the plugin derived from your routes.
Both work. We recommend committing it at first (readable diffs whenever routes change, and CI
typechecks without a generation step). Once you trust the tool, you can gitignore it and run
npx modern typegen in CI instead.
4. Navigate with types
Import navigation APIs from modernjs-typed-routes instead of @modern-js/runtime/router — the
package re-exports everything from the router, with Link, Navigate and useNavigate replaced by
typed versions:
That's the whole setup. See Navigation for the full API.
CI recipe
Catch broken links in pull requests:
Troubleshooting
- Types don't update in the editor — the file regenerated but the TS server cached it: run "TypeScript: Restart TS Server". This is rare; the dev server keeps the file current.
toaccepts any string — theRegisteris empty because types were never generated. Runnpx modern typegenand confirmsrc/routes.gen.d.tsexists and is inside your tsconfiginclude.
More cases (multi-entry, monorepos, custom basename, lint tools) in the full Troubleshooting guide.