A Vite plugin that automatically generates TypeScript types for all your SvelteKit routes, providing type-safe navigation and link generation.
src/routes directory and generates types for all valid routes/about, /contact)/blog/[slug], /users/[id])/[[lang]]/about)/docs/[...path])/(app)/dashboard)/posts/[slug=string])+server.ts files)// vite.config.ts
import { sveltekit } from "@sveltejs/kit/vite";
import { sveltepaths } from "sveltekit-paths";
import { defineConfig } from "vite";
export default defineConfig({
plugins: [
sveltekit(),
sveltepaths(), // Add the plugin
],
});
Important: You need to include the generated type definition file in your TypeScript configuration for the types to be recognized.
Add the generated types file to your tsconfig.json:
{
"compilerOptions": {
"types": [".vite/sveltekit-paths"]
}
}
The plugin creates a module declaration that you can import and use:
// In your Svelte components or TypeScript files
import type { RoutePath } from "$sveltekit-paths";
// Type-safe navigation
import { goto } from "$app/navigation";
function navigateToPost(slug: string) {
goto(`/blog/${slug}` satisfies RoutePath);
}
// Type-safe link creation
function createUserLink(userId: string): RoutePath {
return `/users/${userId}`;
}
<!-- In your Svelte components -->
<script lang="ts">
import type { RoutePath } from "$sveltekit-paths";
export let href: RoutePath;
</script>
<a {href}>Navigate</a>
Given this route structure:
src/routes/
āāā +page.svelte ā /
āāā about/+page.svelte ā /about
āāā blog/
ā āāā +page.svelte ā /blog
ā āāā [slug]/+page.svelte ā /blog/${string}
āāā users/[id]/
ā āāā +page.svelte ā /users/${string}
ā āāā settings/+page.svelte ā /users/${string}/settings
āāā docs/[[version]]/
ā āāā +page.svelte ā /docs, /docs/${string}
ā āāā guide/[[section]]/+page.svelte ā /docs/guide, /docs/${string}/guide, /docs/${string}/guide/${string}
āāā admin/[...path]/+page.svelte ā /admin/${string}
āāā (app)/dashboard/+page.svelte ā /dashboard
āāā api/users/[id]/+server.ts ā /api/users/${string}
The plugin generates:
// .svelte-kit/types/sveltekit-paths/$types.d.ts
declare module "$sveltekit-paths" {
export type RoutePath =
| `/`
| `/about`
| `/blog`
| `/blog/${string}`
| `/users/${string}`
| `/users/${string}/settings`
| `/docs`
| `/docs/${string}`
| `/docs/guide`
| `/docs/${string}/guide`
| `/docs/${string}/guide/${string}`
| `/admin/${string}`
| `/dashboard`
| `/api/users/${string}`;
}
src/routes directory+page.svelte, +server.ts)[param] ā ${string}[[optional]] ā generates multiple route variants[...rest] ā ${string}(groups) ā removed from final paths.svelte-kit/types/sveltekit-paths/The plugin currently uses sensible defaults but scans from src/routes by default. The generated types are placed in .svelte-kit/types/sveltekit-paths/$types.d.ts.
The plugin runs during both development and build:
+page.svelte, +server.ts).svelte-kit/types is included in your tsconfig.json paths