Skip to content

stacksjs/dtsx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Social Card of this repo

npm version GitHub Actions Commitizen friendly

dtsx

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.

Features

  • ⚡ 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

Install

bun install -d @stacksjs/dtsx

@npmjs.com, please allow us to use the dtsx package name 🙏

Get Started

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 } }

Library

Given the npm package is installed, you can use the generate function to generate TypeScript declaration files from your project.

Usage

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 generate

CLI

The dtsx CLI provides a simple way to generate TypeScript declaration files from your project. Here's how to use it:

Generate Command

Generate declaration files using the default options:

dtsx generate

Or 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 --version

Watch Command

Watch for changes and regenerate automatically:

# Watch with default options: dtsx watch # Watch specific directory: dtsx watch --root src --outdir dist/types

Stdin Command

Process 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.ts

Available Options

Basic 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.

Testing

bun test

Changelog

Please see our releases page for more information on what has changed recently.

Contributing

Please review the Contributing Guide for details.

Community

For help, discussion about best practices, or any other conversation that would benefit from being searchable:

Discussions on GitHub

For casual chit-chat with others using this package:

Join the Stacks Discord Server

Postcardware

“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 🌎

Sponsors

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.

Credits

License

The MIT License (MIT). Please see LICENSE for more information.

Made with 💙