vite-dts-plugin
raw JSON → 1.2.0 verified Mon Apr 27 auth: no javascript
Vite plugin to generate TypeScript type definitions (.d.ts) for libraries and applications. Current stable version is 1.2.0, with monthly or bi-monthly releases. Zero-dependency plugin that wraps the TypeScript compiler (tsc) in a Vite plugin, generating declaration files on build. Differentiators: minimal configuration, no external dependencies, supports custom tsc binary, outDir, tsConfig, and additional tsc arguments. Ideal for library authors wanting a lightweight alternative to heavier plugins like vite-plugin-dts.
Common errors
error Error: Cannot find module 'typescript' ↓
cause TypeScript is not installed.
fix
Run 'npm install --save-dev typescript'.
error Error: tsc: command not found ↓
cause TypeScript binary not in PATH or not installed.
fix
Install TypeScript and ensure node_modules/.bin is in PATH, or specify a custom tsc path with the 'tsc' option.
error TypeError: dtsPlugin is not a function ↓
cause Importing incorrectly, e.g., default import instead of named import.
fix
Use named import: import { dtsPlugin } from 'vite-dts-plugin'.
Warnings
gotcha The outDir option must match the directory where your built output (JS) is placed, otherwise types may reference missing paths. ↓
fix Ensure outDir is same as Vite's build.outDir (default 'dist').
gotcha The plugin runs tsc under the hood; it does not run Vite's build itself. You must have TypeScript installed separately. ↓
fix Add 'typescript' as a devDependency.
gotcha Custom tsConfig option expects a string path, not a parsed config object. Passing an object will throw. ↓
fix Pass a string filename (e.g., 'tsconfig.build.json') or leave undefined to use default tsconfig.
gotcha The args option is passed as additional CLI arguments to tsc; ensure they are valid tsc flags. Invalid flags cause silent failures. ↓
fix Only pass known tsc options, e.g., ['--declaration', '--emitDeclarationOnly'].
Install
npm install vite-dts-plugin yarn add vite-dts-plugin pnpm add vite-dts-plugin Imports
- dtsPlugin wrong
const dtsPlugin = require('vite-dts-plugin').dtsPlugincorrectimport { dtsPlugin } from 'vite-dts-plugin' - Options wrong
import { Options } from 'vite-dts-plugin'correctimport type { Options } from 'vite-dts-plugin'
Quickstart
// vite.config.ts
import { defineConfig } from 'vite';
import { dtsPlugin } from 'vite-dts-plugin';
export default defineConfig({
build: {
lib: {
entry: 'src/index.ts',
formats: ['es'],
},
},
plugins: [
dtsPlugin({
outDir: 'dist',
tsc: 'tsc',
}),
],
});