I want to make the routes passed to the Angular Router a constant so that I can have IntelliSense for all possible route paths:
const route = [
{ path: 'my-path', component: MenuItemsComponent },
{ path: 'my-path', component: MenuItemsComponent },
] as const;
const myRoutes = route as Routes;
Typescript says I can't do this, it throws this error:
Conversion of type 'readonly [{ readonly path: "my-path"; readonly component: typeof MenuItemsComponent; }, { readonly path: "my-path"; readonly component: typeof MenuItemsComponent; }]' to type 'Routes' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
The type 'readonly [{ readonly path: "my-path"; readonly component: typeof MenuItemsComponent; }, { readonly path: "my-path"; readonly component: typeof MenuItemsComponent; }]' is 'readonly' and cannot be assigned to the mutable type 'Routes'.
But why is Routes a mutable type? I can't understand.
Edit
To explain why i need const, is because in with consts we can do that:
type Path = typeof route[number]['path'];
So now i can restrict a variable to a path declared in the router.
