A library that helps you generate TypeScript declaration files from your project. Given we do not know the user's input ever, we need to never hardcode based results based from our examples, always create a dynamic solution.
- ⚡ Extremely fast .d.ts generation
- 🔄 Parallel processing support
- 📥 Stdin/stdout support for piping
- ⚙️ Highly configurable
- 🪶 Lightweight library
- 🤖 Cross-platform binary
- 👀 Watch mode for development
- ✅ Built-in validation
bun install -d @stacksjs/dtsx@npmjs.com, please allow us to use the dtsx package name 🙏
There are two ways of using this ".d.ts generation" tool: as a library or as a CLI.
But before you get started, please ensure you enabled isolatedDeclarations in your tsconfig.json file.
{ "compilerOptions": { "isolatedDeclarations": true } }Given the npm package is installed, you can use the generate function to generate TypeScript declaration files from your project.
import type { DtsGenerationOptions } from '@stacksjs/dtsx' import { generate, processSource } from '@stacksjs/dtsx' const options: DtsGenerationOptions = { cwd: './', // default: process.cwd() root: './src', // default: './src' entrypoints: ['**/*.ts'], // default: ['**/*.ts'] outdir: './dist', // default: './dist' clean: true, // default: false verbose: true, // default: false keepComments: true, // default: true // New options: parallel: true, // default: false - process files in parallel concurrency: 4, // default: 4 - number of concurrent workers dryRun: false, // default: false - preview without writing stats: true, // default: false - show generation statistics validate: true, // default: false - validate generated .d.ts files } const stats = await generate(options) console.log(`Generated ${stats.filesGenerated} files in ${stats.durationMs}ms`) // You can also process source code directly: const dtsContent = processSource(` export const greeting: string = "Hello"; export function greet(name: string): string { return greeting + " " + name; } `) console.log(dtsContent) // Output: // export declare const greeting: string; // export declare function greet(name: string): string;Available options:
Library usage can also be configured using a dts.config.ts (or dts.config.js) file which is automatically loaded when running the ./dtsx (or bunx dtsx) command. It is also loaded when the generate function is called, unless custom options are provided.
// dts.config.ts (or dts.config.js) export default { cwd: './', root: './src', entrypoints: ['**/*.ts'], outdir: './dist', keepComments: true, clean: true, verbose: true, // Performance options parallel: true, concurrency: 4, // Output options stats: true, validate: true, // Filtering exclude: ['**/*.test.ts', '**/__tests__/**'], importOrder: ['node:', 'bun', '@myorg/'], }You may also run:
./dtsx generate # if the package is installed, you can also run: # bunx dtsx generateThe dtsx CLI provides a simple way to generate TypeScript declaration files from your project. Here's how to use it:
Generate declaration files using the default options:
dtsx generateOr use custom options:
# Generate declarations for specific entry points: dtsx generate --entrypoints src/index.ts,src/utils.ts --outdir dist/types # Generate declarations with custom configuration: dtsx generate --root ./lib --outdir ./types --clean # Use parallel processing for large projects: dtsx generate --parallel --concurrency 8 # Preview what would be generated (dry run): dtsx generate --dry-run --stats # Validate generated declarations: dtsx generate --validate # Exclude test files: dtsx generate --exclude "**/*.test.ts,**/__tests__/**" # Custom import ordering: dtsx generate --import-order "node:,bun,@myorg/" dtsx --help dtsx --versionWatch for changes and regenerate automatically:
# Watch with default options: dtsx watch # Watch specific directory: dtsx watch --root src --outdir dist/typesProcess TypeScript from stdin and output declarations to stdout:
# Pipe source code directly: echo "export const foo: string = 'bar'" | dtsx stdin # Process a file through stdin: cat src/index.ts | dtsx stdin # Chain with other tools: cat src/utils.ts | dtsx stdin > dist/utils.d.tsBasic Options:
--cwd <path>: Set the current working directory (default: current directory)--root <path>: Specify the root directory of the project (default: './src')--entrypoints <files>: Define entry point files (comma-separated, default: '**/*.ts')--outdir <path>: Set the output directory for generated .d.ts files (default: './dist')--keep-comments: Keep comments in generated .d.ts files (default: true)--clean: Clean output directory before generation (default: false)--tsconfig <path>: Specify the path to tsconfig.json (default: 'tsconfig.json')
Performance Options:
--parallel: Process files in parallel (default: false)--concurrency <number>: Number of concurrent workers with --parallel (default: 4)
Output Options:
--verbose: Enable verbose output (default: false)--log-level <level>: Log level: debug, info, warn, error, silent (default: 'info')--stats: Show statistics after generation (default: false)--output-format <format>: Output format: text or json (default: 'text')--progress: Show progress during generation (default: false)--diff: Show diff of changes compared to existing files (default: false)
Validation Options:
--validate: Validate generated .d.ts files against TypeScript (default: false)--continue-on-error: Continue processing if a file fails (default: false)--dry-run: Preview without writing files (default: false)
Filtering Options:
--exclude <patterns>: Glob patterns to exclude (comma-separated)--import-order <patterns>: Import order priority patterns (comma-separated)
To learn more, head over to the documentation.
bun testPlease see our releases page for more information on what has changed recently.
Please review the Contributing Guide for details.
For help, discussion about best practices, or any other conversation that would benefit from being searchable:
For casual chit-chat with others using this package:
Join the Stacks Discord Server
“Software that is free, but hopes for a postcard.” We love receiving postcards from around the world showing where dtsx is being used! We showcase them on our website too.
Our address: Stacks.js, 12665 Village Ln #2306, Playa Vista, CA 90094, United States 🌎
We would like to extend our thanks to the following sponsors for funding Stacks development. If you are interested in becoming a sponsor, please reach out to us.
The MIT License (MIT). Please see LICENSE for more information.
Made with 💙
