
Product
Introducing Socket Firewall Enterprise: Flexible, Configurable Protection for Modern Package Ecosystems
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.
vite-plugin-typed-router
Advanced tools
Provide autocompletion for pages route names generated by vite-plugin-pages router
Provide a type safe router to any app with auto-generated typed definitions for route names and autocompletion for route params
Support for now:
| Framework | Support |
|---|---|
| Vue | β |
| React | β |
| Svelte | β |
useTypedRouter composable that returns the typed router and an object containing a tree of your routespush and replace methodspnpm i -D vite-plugin-typed-router vue-router
# or
npm install -D vite-plugin-typed-router vue-router
First, register the module in the vite.config.ts
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import TypedRouter from 'vite-plugin-typed-router';
export default defineConfig({
plugins: [vue(), TypedRouter(/* Options */)],
});
In your main.ts, register the routes
import { createApp } from 'vue';
import App from './App.vue';
import { createRouter, createWebHistory } from 'vue-router';
// Generated routes
import routes from '~pages';
const router = createRouter({
history: createWebHistory(),
routes,
});
const app = createApp(App);
app.use(router);
app.mount('#app');
export interface TypedRouterOptions {
/** Location of your src directory
* @default "src"
*/
srcDir?: string;
/** Output directory where you cant the files to be saved (ex: "./generated")
* @default "<srcDir>/generated"
*/
outDir?: string;
/** Location of your pages directory
* @default "<srcDir>/pages"
*/
pagesDir?: string;
/** Print the generated routes tree object in output
* @default "true"
*/
printRoutesTree?: boolean;
}
The module will generate 4 files each time you modify the pages folder :
~/<outDir>/__routes.ts with the global object of the route names inside.~/<outDir>/__useTypedRouter.ts Composable tu simply access your typed routes~/<outDir>/typed-router.d.ts containing the global typecript definitions and exportsYou can specify the output dir of the generated files in your configuration. It defaults to <srcDir>/generated
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import TypedRouter from 'vite-plugin-typed-router';
export default defineConfig({
plugins: [vue(), TypedRouter({outDir: 'generated'})],
});
Given this structure
βββ pages
βββ index
βββ content
βββ [id].vue
βββ content.vue
βββ index.vue
βββ communication.vue
βββ statistics.vue
βββ [user].vue
βββ index.vue
βββ forgotpassword.vue
βββ reset-password.vue
β βββ login.vue
βββ ...
The generated route list will look like this
export const routesNames = {
forgotpassword: 'forgotpassword' as const,
login: 'login' as const,
resetPassword: 'reset-password' as const,
index: {
index: 'index' as const,
communication: 'index-communication' as const,
content: {
id: 'index-content-id' as const,
},
statistics: 'index-statistics' as const,
user: 'index-user' as const,
},
};
export type TypedRouteList =
| 'forgotpassword'
| 'login'
| 'reset-password'
| 'index'
| 'index-communication'
| 'index-content-id'
| 'index-statistics'
| 'index-user';
You can disable
routesNamesobject generation if you don't need it with theprintRoutesTreeoption
useTypedRouter hookuseTypedRouter is an exported composable from nuxt-typed-router. It contains a clone of vue-router but with strictly typed route names and params type-check
<script lang="ts">
// The path here is `~/generated` because I set `outDir: './generated'` in my module options
import { useTypedRouter } from '~/generated';
export default defineComponent({
setup() {
// Fully typed
const { router, routes } = useTypedRouter();
function navigate() {
// Autocompletes the name and infer the params
router.push({ name: routes.index.user, params: { user: 1 } }); // β
valid
router.push({ name: routes.index.user, params: { foo: 1 } }); // β invalid
router.push({ name: 'index-user', params: { user: 1 } }); // β
valid
router.push({ name: 'index-user', params: { foo: 1 } }); // β invalid
router.push({ name: 'index-foo-bar' }); // β invalid
}
return { navigate };
},
});
</script>
FAQs
Provide autocompletion for pages route names generated by vite-plugin-pages router
We found that vite-plugin-typed-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.Β It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.

Security News
Open source dashboard CNAPulse tracks CVE Numbering Authoritiesβ publishing activity, highlighting trends and transparency across the CVE ecosystem.

Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socketβs new workflow scanning support.