ng-typed-routes
Angular routes are strings, and the compiler has never once checked one. Rename a
route and every routerLink pointing at the old name keeps building, keeps deploying, and fails
in front of whoever clicks it first. This generates a typed tree from the app.routes.ts you
already have, so those links turn red in your editor instead.
Rename a route. See who notices.
A routing library has nothing to show in a running app — the whole point happens in your editor, before the app runs at all. So here is the editor. Rename the route, then switch between the two approaches.
The error text, codes and line numbers are the real ones — TS2339 from the TypeScript compiler, NG04002 from the Angular router.
It reads the routes you already wrote
No new routing config, no second source of truth. The generator walks your route tree with the
TypeScript compiler API — through children, spreads, identifiers and every
loadChildren: () => import(...) form — and writes one file.
routes.users.detail // RouteDef<'/users/:id'> routes.users.detail.orders.path({ id }) // '/users/1/orders'
const router = inject(TypedRouter); router.navigate(routes.users.detail.orders, { id, query: { status: 'open' }, }); const { id } = injectParams(routes.users.detail); // Signal<string> const { status } = injectQuery( routes.users.detail.orders );
Everything is a key on a generated tree rather than a string in an array, which is the entire trick: TypeScript already knows how to tell you a property does not exist.
Params are signals, read straight from the route definition, so the type of
id comes from the same place the URL does.
The generated file is committed, so a reviewer can see routing change in the diff.
npm run routes:check exits 1 when it is stale, which is the one
line you add to CI.
Small apps without lazy routes can skip the codegen entirely —
typedRoutes(defineRoutes([...])) builds the same tree from
literal types at compile time.
One command, then one script in CI
ng add ng-typed-routes
# write the tree ng-typed-routes generate # regenerate as you edit routes ng-typed-routes watch # exits 1 when routes.gen.ts is stale ng-typed-routes check
ng add adds the three scripts, writes
src/app/routes.gen.ts and leaves the rest of your app alone.
Paths, the output file, the tsconfig and the export name are all flags, or they can live in
ng-typed-routes.config.json if you would rather not repeat
them.
Routes that are not navigation targets — ** and
redirectTo — are skipped. Custom matchers and non-literal
paths are skipped too, with a warning rather than in silence.
What your paths become
Predictable enough that you can guess a key without opening the generated file.
| Angular path | Generated key |
|---|---|
users |
routes.users |
user-settings |
routes.userSettings |
:id child of users |
routes.users.$id |
'' at the root |
routes.index |
'' with children, a layout route |
transparent — children are hoisted |
admin/reports |
routes.admin.reports |
{ path: 'chat', outlet: 'aux' } |
routes.aux.chat |
**, redirectTo |
skipped — not navigation targets |
{ path: ':id', component: UserDetail }
satisfies TypedRoute<{
name: 'detail';
query: { tab?: 'open' | 'closed' };
}>
A type-only satisfies, so it disappears at build time and the route object
stays an ordinary route object.
What doesn't work yet
It is young, and I would rather you read this here than find it on a Tuesday afternoon.
Template [params] is not checked
path(), TypedRouter.navigate(), injectParams and
route-name typos are all compile errors today, and verified as such. The
[typedLink]/[params] pair checks correctly at the TypeScript level, but
Angular's template compiler currently accepts a missing or wrong [params]. Being worked
on.
Query values are strings
You can declare the shape, and the keys are checked. There is no runtime validation and no coercion — a number comes back as the string it was in the URL.
Named outlets stop at the top
Children of a named outlet are not generated, and path() falls back to
Angular's own (outlet:segment) URL syntax.
Free to use, MIT licensed
Early days, so issues and pull requests are especially welcome — particularly if you have a route shape the generator trips over.