router icon indicating copy to clipboard operation
router copied to clipboard

Typed routes out of the box (without file based routing)

Open ThomasKientz opened this issue 7 months ago • 1 comments

What problem is this solving

Navigate to an unknown route isn't catch by typescript.

export const router = createRouter({
  routes: [
    { path: "/foo", component: () => import("./pages/foo.vue") },
  ],
  history: createWebHistory(),
});

router.push("/bar"); // <------ No type error

Proposed solution

Infer types so router is strongly typed with declared routes.

Describe alternatives you've considered

unplugin-vue-router but its file based routing.

ThomasKientz avatar Apr 21 '25 11:04 ThomasKientz

Hello!

I just recently made a library that might help you - https://github.com/dmitry-lavrik/vue-routes-to-types

It outputs types for RouteNamedMap from routes array.

Hopefully, someday such functionality will be created in the Vue-router core. Or they will put a link from the documentation to this library :)

dmitry-lavrik avatar May 28 '25 14:05 dmitry-lavrik