DTS Bundle Generator

A tool to generate a single bundle of dts with types tree-shaking


DTS Bundle Generator

Small tool to generate a dts bundle from your ts code.

For example:

  1. ```ts
  2. // a.ts
  3. export class A {}
  4. ```

  1. ```ts
  2. // b.ts
  3. export class B {}
  4. ```

  1. ```ts
  2. // entry.ts
  3. import { A } from './a';
  4. import { B } from './b';

  5. declare function makeA(): A;
  6. export function makeB(): B {
  7.     makeA();
  8.     return new B();
  9. }
  10. ```

When you run dts-bundle-generator -o my.d.ts entry.ts in my.d.ts you will get the following:

  1. ```ts
  2. declare class B {
  3. }
  4. export declare function makeB(): B;
  5. ```


1. Install the package from npm:

    npm install --save-dev dts-bundle-generator


    npm install -g dts-bundle-generator

1. Enable declaration compiler option in tsconfig.json


  1. ```
  2. Usage: dts-bundle-generator [options] <file(s)>

  3. Options:
  4.   --help                         Show help                                                 [boolean]
  5.   --out-file, -o                 File name of generated d.ts                                [string]
  6.   --verbose                      Enable verbose logging                   [boolean] [default: false]
  7.   --silent                       Disable any logging except errors        [boolean] [default: false]
  8.   --no-check                     Skip validation of generated d.ts file   [boolean] [default: false]
  9.   --fail-on-class                Fail if generated dts contains class declaration
  10.                                                                           [boolean] [default: false]
  11.   --external-inlines             Array of package names from node_modules to inline typings from.
  12.                                  Used types will be inlined into the output file             [array]
  13.   --external-imports             Array of package names from node_modules to import typings from.
  14.                                  Used types will be imported using "import { First, Second } from
  15.                                  'library-name';".
  16.                                  By default all libraries will be imported (except inlined libraries
  17.                                  and libraries from @types)                                  [array]
  18.   --external-types               Array of package names from @types to import typings from via the
  19.                                  triple-slash reference directive.
  20.                                  By default all packages are allowed and will be used according to
  21.                                  their usages                                                [array]
  22.   --umd-module-name              Name of the UMD module. If specified then `export as namespace
  23.                                  ModuleName;` will be emitted                               [string]
  24.   --project                      Path to the tsconfig.json file that will be used for the
  25.                                  compilation                                                [string]
  26.   --sort                         Sort output nodes                        [boolean] [default: false]
  27.   --inline-declare-global        Enables inlining of `declare global` statements contained in files
  28.                                  which should be inlined (all local files and packages from
  29.                                  `--external-inlines`)                    [boolean] [default: false]
  30.   --inline-declare-externals     Enables inlining of `declare module` statements of the global
  31.                                  modules (e.g. `declare module 'external-module' {}`, but NOT
  32.                                  `declare module './internal-module' {}`) contained in files which
  33.                                  should be inlined (all local files and packages from inlined
  34.                                  libraries)                               [boolean] [default: false]
  35.   --disable-symlinks-following   (EXPERIMENTAL) Disables resolving of symlinks to the original path.
  36.                                  See https://github.com/timocov/dts-bundle-generator/issues/39 for
  37.                                  more information                         [boolean] [default: false]
  38.   --respect-preserve-const-enum  Enables stripping the `const` keyword from every direct-exported
  39.                                  (or re-exported) from entry file `const enum`. See
  40.                                  https://github.com/timocov/dts-bundle-generator/issues/110 for more
  41.                                  information                              [boolean] [default: false]
  42.   --export-referenced-types      By default all interfaces, types and const enums are marked as
  43.                                  exported even if they aren't exported directly. This option allows
  44.                                  you to disable this behavior so a node will be exported if it is
  45.                                  exported from root source file only.      [boolean] [default: true]
  46.   --config                       File path to the generator config file                     [string]
  47.   --no-banner                    Allows remove "Generated by dts-bundle-generator" comment from the
  48.                                  output                                   [boolean] [default: false]
  49.   --version                      Show version number                                       [boolean]
  50. ```


  1. ```bash
  2. ./node_modules/.bin/dts-bundle-generator -o my.d.ts path/to/your/entry-file.ts
  3. ```

  1. ```bash
  2. ./node_modules/.bin/dts-bundle-generator path/to/your/entry-file.ts path/to/your/entry-file-2.ts
  3. ```

  1. ```bash
  2. ./node_modules/.bin/dts-bundle-generator --external-inlines=@mycompany/internal-project --external-imports=@angular/core rxjs path/to/your/entry-file.ts
  3. ```

  1. ```bash
  2. ./node_modules/.bin/dts-bundle-generator --external-types=jquery path/to/your/entry-file.ts
  3. ```

Config file

It is unnecessary, but you can use config file for the tool. See config documentation for more information.


If you have modules then you can create definitions by default using tsc, but tsc generates them for each module separately.
Yeah, you can use outFile (for amd and system), but generated code looks like this:

  1. ```ts
  2. declare module "a" {
  3.     export class A {
  4.     }
  5. }
  6. declare module "b" {
  7.     export class B {
  8.     }
  9. }
  10. declare module "entry" {
  11.     import { B } from "b";
  12.     export function makeB(): B;
  13. }
  14. ```


1. A is not used at all and most probably you do not want to export it.
1. If you bundle your code in a way when all modules are merged (like when using Webpack or Rollup) then there should be no such modules as a or b (actually entry too) in the resulting file.

Known limitations

1. All your types should have different names inside a bundle. If you have 2 interface Options {} they will be merged by TypeScript and you will get wrong definitions (see https://github.com/timocov/dts-bundle-generator/issues/116 and https://github.com/timocov/dts-bundle-generator/issues/130)
1. Importing and exporting with renaming in modules outside of entry points is limited/not supported as yet (see https://github.com/timocov/dts-bundle-generator/issues/184)