Skip to content

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.

json
{
  "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

ts
import type { DtsGenerationOptions } from '@stacksjs/dtsx'
import { generate } 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, // coming soon
}

await generate(options)

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.

ts
// dts.config.ts (or dts.config.js)

export default {
  cwd: './',
  root: './src',
  entrypoints: ['**/*.ts'],
  outdir: './dist',
  keepComments: true,
  clean: true,
  verbose: true,
}

You may also run:

bash
./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:

Usage

Generate declaration files using the default options:

bash
dtsx generate

Or use custom options:

bash
# 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

dtsx --help
dtsx --version

Available 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')
  • --verbose: Enable verbose output (default: false)

To learn more, head over to the documentation.

Released under the MIT License.