A Vite plugin that generates TypeScript declaration files from source files in library mode
—
Pre-configured resolvers for common file types including JSON data files, Vue Single File Components, and Svelte components. These resolvers are automatically included with the plugin and handle specialized file types during TypeScript declaration generation.
Automatically handles JSON data files by generating appropriate TypeScript declarations for imported JSON modules.
// JSON files are automatically processed - no configuration needed
// The plugin generates declarations for .json importsUsage Examples:
import dts from "vite-plugin-dts";
// JSON resolver works automatically
export default defineConfig({
plugins: [
dts({
// JSON files are automatically handled
include: ["src/**/*.ts", "src/**/*.json"]
})
]
});
// Example JSON file: config.json
// {
// "apiUrl": "https://api.example.com",
// "timeout": 5000,
// "features": ["auth", "analytics"]
// }
// When imported: import config from './config.json'
// Generated declaration: config.json.d.ts
// declare const _default: {
// "apiUrl": "https://api.example.com",
// "timeout": 5000,
// "features": ["auth", "analytics"]
// };
//
// export default _default;Automatically processes Vue Single File Components (.vue files) to generate TypeScript declarations using Vue language tools integration.
// Vue SFC files are automatically processed when included
// Uses @vue/language-core and @volar/typescript internallyUsage Examples:
import dts from "vite-plugin-dts";
// Vue resolver works automatically
export default defineConfig({
plugins: [
dts({
// Vue SFC files are automatically handled
include: ["src/**/*.ts", "src/**/*.vue"],
cleanVueFileName: true // Optional: transform .vue.d.ts to .d.ts
})
]
});
// Example Vue component: Button.vue
// <template>
// <button :class="buttonClass" @click="handleClick">
// <slot />
// </button>
// </template>
//
// <script setup lang="ts">
// interface Props {
// variant?: 'primary' | 'secondary';
// disabled?: boolean;
// }
//
// const props = withDefaults(defineProps<Props>(), {
// variant: 'primary',
// disabled: false
// });
//
// const emit = defineEmits<{
// click: [event: MouseEvent];
// }>();
//
// const buttonClass = computed(() => `btn btn-${props.variant}`);
// const handleClick = (event: MouseEvent) => emit('click', event);
// </script>
// Generated declaration: Button.vue.d.ts (or Button.d.ts with cleanVueFileName)
// The exact output depends on Vue language tools processingAutomatically handles Svelte component files (.svelte) to generate appropriate TypeScript declarations. The resolver detects the Svelte version and exports the correct component type.
// Svelte component files are automatically processed when included
// For Svelte < 4.0: exports SvelteComponentTyped
// For Svelte >= 4.0: exports SvelteComponentUsage Examples:
import dts from "vite-plugin-dts";
// Svelte resolver works automatically
export default defineConfig({
plugins: [
dts({
// Svelte component files are automatically handled
include: ["src/**/*.ts", "src/**/*.svelte"]
})
]
});
// Example Svelte component: Card.svelte
// <script lang="ts">
// export let title: string;
// export let description: string = '';
// export let variant: 'default' | 'highlighted' = 'default';
// </script>
//
// <div class="card {variant}">
// <h3>{title}</h3>
// {#if description}
// <p>{description}</p>
// {/if}
// <slot />
// </div>
// Generated declaration: Card.svelte.d.ts
// export { SvelteComponent as default } from 'svelte';
// (or SvelteComponentTyped for Svelte < 4.0)Built-in resolvers are automatically integrated into the plugin's processing pipeline alongside any custom resolvers.
/**
* Built-in resolvers are automatically included:
* - JSON resolver (name: 'json') for .json files
* - Vue resolver (name: 'vue') for .vue files
* - Svelte resolver (name: 'svelte') for .svelte files
*/Usage Examples:
// Built-in resolvers work alongside custom ones
dts({
resolvers: [
// Custom resolver for additional file types
{
name: "yaml",
supports: (id) => id.endsWith(".yaml"),
transform: async ({ id }) => {
return [{
path: id.replace(/\.yaml$/, ".d.ts"),
content: "export declare const config: Record<string, any>;"
}];
}
}
// Built-in JSON, Vue, and Svelte resolvers remain active
]
});
// Override built-in resolver with custom implementation
dts({
resolvers: [
{
name: "json", // Same name as built-in resolver - overrides it
supports: (id) => id.endsWith(".json"),
transform: async ({ id }) => {
return [{
path: id.replace(/\.json$/, ".d.ts"),
content: `declare const data: any;\nexport default data;`
}];
}
}
]
});Optimizing built-in resolver behavior for specific frontend framework setups.
// Vue 3 + TypeScript project configuration
const vueConfig = {
include: ["src/**/*.ts", "src/**/*.vue"],
exclude: ["src/**/*.test.ts"],
cleanVueFileName: true, // Transform .vue.d.ts -> .d.ts
compilerOptions: {
jsx: "preserve",
jsxImportSource: "vue"
}
};
// Svelte + TypeScript project configuration
const svelteConfig = {
include: ["src/**/*.ts", "src/**/*.svelte"],
exclude: ["src/**/*.test.ts", "src/**/*.spec.ts"],
compilerOptions: {
moduleResolution: "node",
target: "ESNext"
}
};Complete Configuration Examples:
// Vue 3 library with built-in resolver
export default defineConfig({
build: {
lib: {
entry: "src/index.ts",
formats: ["es", "cjs"]
}
},
plugins: [
vue(),
dts({
include: ["src/**/*.ts", "src/**/*.vue"],
cleanVueFileName: true,
rollupTypes: true
})
]
});
// Svelte component library with built-in resolver
export default defineConfig({
build: {
lib: {
entry: "src/index.ts",
formats: ["es"]
}
},
plugins: [
svelte(),
dts({
include: ["src/**/*.ts", "src/**/*.svelte"],
outDir: "dist/types"
})
]
});
// Multi-framework project with all built-in resolvers
export default defineConfig({
plugins: [
vue(),
svelte(),
dts({
include: ["src/**/*.ts", "src/**/*.vue", "src/**/*.svelte", "src/**/*.json"],
outDir: ["dist/types", "lib/types"]
})
]
});The built-in resolvers are implemented internally using the plugin's resolver system:
// Internal resolver structure (not directly accessible)
interface InternalResolver {
name: string; // 'json', 'vue', or 'svelte'
supports: (id: string) => boolean;
transform: (payload: ResolverPayload) => ResolverResult;
}
// These are automatically instantiated and included by the plugin
// Users cannot directly import or configure these specific instancesInstall with Tessl CLI
npx tessl i tessl/npm-vite-plugin-dts