Export single .d.ts from several typescript files + entrypoint
Asked Answered
A

4

22

I've several .ts files + 1 entrypoint like so:

  • classA.ts
  • classB.ts
  • entrypoint.ts

entrypoint.ts contains something similar to:

export * from './classA';
export * from './classB';

I'd like to have a single .d.ts describing everything entrypoint exports so that both ClassA and ClassB definition files are included.

Apollonius answered 27/9, 2016 at 10:48 Comment(0)
P
13

You cannot auto-generate a single d.ts file. What works fine is the following (assuming you are building a library / reusable module):

  1. have the compiler auto-generate declarations for your classes by specifying "declaration": true in tsconfig.json

  2. the compiler will also generate an entrypoint.d.ts file (that re-exports the d.tsof classA and classB)

  3. Point the typings entry of your package.json to entrypoint.d.ts e.g. "typings": "./dist/entrypoint.d.ts"

Assuming your library is called library, you can now install it in the node_modules of a project and use it with a standard import:

import {classA, classB} from 'library'

will import the generated d.ts. for those classes.

Pleomorphism answered 27/9, 2016 at 12:57 Comment(1)
Step 2 did not happen for me. The index.d.ts file (which exports all the other *.ts files in the project simply had the line "export {};"Triplenerved
T
3

For anyone who finds this post. You can try npm-dts utility. This should do the trick.

Transcript answered 14/1, 2020 at 8:6 Comment(0)
W
2

This one worked for me and it was pretty straight forward: https://github.com/TypeStrong/dts-bundle

I tried these without luck:

Washrag answered 7/3, 2019 at 1:5 Comment(0)
N
0

Update for 2024: dts-bundle and npm-dts seem to be outdated

This one is alive and worked for me: https://github.com/timocov/dts-bundle-generator

Generate bundle.d.ts form .ts directly you sometimes need to fix Typescript errors first. like importing global declarations and outdated properties.

Nariko answered 3/4, 2024 at 9:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.