For AI agents: the complete documentation index is available at https://rslib.rs/llms.txt, the full documentation bundle is available at https://rslib.rs/llms-full.txt, and this page is available as Markdown at https://rslib.rs/guide/advanced/dts.md.
close
  • English
  • Declaration files

    This chapter introduces what TypeScript Declaration Files are and how to generate declaration files in Rslib.

    What is declaration files

    TypeScript Declaration Files provide type information for JavaScript code. Declaration files typically have a .d.ts extension. They allow the TypeScript compiler to understand the type structure of JavaScript code, enabling features like:

    1. Type Checking: Provide type information for JavaScript code, helping developers catch potential type errors at compile time.
    2. Code Completion: Enhance code editor features like autocomplete and code navigation.
    3. Documentation Generation: Generate documentation for JavaScript code, providing better developer experience.
    4. IDE Support: Improve the developer experience in IDEs like Visual Studio Code, WebStorm, and others.
    5. Library Consumption: Make it easier for users to use and understand your library.

    What are bundle declaration files and bundleless declaration files

    Bundle declaration files

    Bundle declaration files involves bundling multiple TypeScript declaration files into a single declaration file.

    • Pros:

      • Simplified Management: Simplifies the management and referencing of type files.
      • Easy Distribution: Reduces the number of files users need to handle when using the library.
    • Cons:

      • Complex Generation: Generating and maintaining a single bundle file can become complex in large projects.
      • Debugging Challenges: Debugging type issues may not be as intuitive as with separate files.

    Bundleless declaration files

    Bundleless declaration files involves generating a separate declaration file for each module in the library, just like tsc does.

    • Pros:

      • Modular: Each module has its own type definitions, making maintenance and debugging easier.
      • Flexibility: Suitable for large projects, avoiding the complexity of a single file.
    • Cons:

      • Multiple Files: Users may need to handle multiple declaration files when using the library.
      • Complex Management: May require additional configuration to correctly reference all files.

    How to generate declaration files in Rslib

    Rslib's declaration generation flow can be split into two steps:

    1. Type generation, that is, generate bundleless declaration files. Rslib supports the following three methods:
    2. Type bundling, that is, generate bundled declaration files (optional). When dts.bundle is enabled, Rslib bundles the generated declaration files with API Extractor.

    Generate bundleless declaration files

    Bundleless declaration files can be generated in the following three ways:

    MethodTypeScript versionType checkingOutput scopeSpeed
    TypeScript API5/6, 7.1+YesDetermined by tsconfig.jsonSlower on 5/6, fast on 7.1+
    TypeScript executable7.0YesDetermined by tsconfig.jsonFast
    isolatedDeclarationsAnyNoModules included in the build dependency graphFastest

    TypeScript API

    TypeScript 5/6 use the TypeScript Compiler API. Like running tsc, it generates declaration files and performs type checking, but is relatively slower.

    TypeScript 7.1 and later use the JavaScript API of native TypeScript for faster declaration generation.

    Enable dts in rslib.config.ts to generate declaration files:

    rslib.config.ts
    export default {
      lib: [
        {
          dts: true,
          // or: dts: { bundle: false }
        },
      ],
    };

    Install TypeScript 7 or later to use native TypeScript for faster declaration generation.

    npm
    yarn
    pnpm
    bun
    deno
    npm add typescript@latest -D

    To use native TypeScript in VS Code, install the TypeScript 7 extension and enable the following setting:

    .vscode/settings.json
    {
      "js/ts.experimental.useTsgo": true
    }

    TypeScript executable

    With TypeScript 7.0, Rslib uses the native TypeScript executable to generate declaration files.

    isolatedDeclarations

    Enabling dts.isolated uses Rspack's built-in SWC fast_dts capability to generate declaration files. This method is the fastest, but it does not perform type checking and only emits declaration files for modules included in the build dependency graph.

    rslib.config.ts
    export default {
      lib: [
        {
          dts: {
            isolated: true,
          },
        },
      ],
    };

    When enabling this option, we recommend also enabling isolatedDeclarations in tsconfig.json:

    tsconfig.json
    {
      "compilerOptions": {
        "isolatedDeclarations": true
      }
    }

    Generate bundle declaration files

    1. Install @microsoft/api-extractor as a development dependency, which is the underlying tool used for bundling declaration files.
    npm
    yarn
    pnpm
    bun
    deno
    npm add @microsoft/api-extractor -D
    1. Set dts.bundle to true in the Rslib config file:
    rslib.config.ts
    export default {
      lib: [
        {
          dts: {
            bundle: true,
          },
        },
      ],
    };
    Note

    Starting with @microsoft/api-extractor 7.60.0, its bundled TypeScript used to analyze declarations is upgraded to v6. Refer to the TypeScript 6.0 migration notes to ensure your project configuration meets TypeScript 6 requirements.

    For example, TypeScript 6.0 no longer automatically includes all @types packages by default. If the declarations being bundled use NodeJS types, explicitly add "node" in tsconfig.json:

    tsconfig.json
    {
      "compilerOptions": {
        "types": ["node"]
      }
    }

    Notes

    During the generation of declaration files, Rslib will automatically enforce some configuration options in tsconfig.json to ensure that TypeScript generates only declaration files.

    {
      "compilerOptions": {
        "noEmit": false,
        "declaration": true,
        "emitDeclarationOnly": true
      }
    }

    The priority from highest to lowest of final output directory of declaration files:

    Content mappers

    Rslib supports Content Mappers, introduced in TypeScript 7.1, for declaration generation.

    Install a content mapper and register the file extensions it handles in tsconfig.json. For example, assuming you have installed a CSS Modules mapper named example-css-modules-mapper (an illustrative package name):

    tsconfig.json
    {
      "contentMappers": [
        {
          "package": "example-css-modules-mapper",
          "extensions": [".module.css"]
        }
      ]
    }

    During declaration generation, Rslib automatically loads the mapper to generate types from CSS Modules class names.

    Configuration itemDescription
    dts.bundleWhether to bundle the declaration files.
    dts.distPathThe output directory of declaration files.
    dts.buildWhether to generate declaration files with building the project references.
    dts.abortOnErrorWhether to abort the build process when an error occurs during declaration files generation.
    dts.autoExtensionWhether to automatically set the declaration file extension based on the format option.
    dts.aliasThe path alias of the declaration files.
    dts.isolatedWhether to generate declaration files with isolatedDeclarations.
    dts.tsgoWhether to generate declaration files with native TypeScript.
    banner.dtsInject content into the top of each declaration output file.
    footer.dtsInject content into the bottom of each declaration file.
    redirect.dts.pathWhether to automatically redirect the import paths of TypeScript declaration output files.
    redirect.dts.extensionWhether to automatically redirect the file extension to import paths based on the TypeScript declaration output files.