Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export single .d.ts from several typescript files + entrypoint

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.

like image 518
Andrea Baccega Avatar asked Sep 27 '16 10:09

Andrea Baccega


2 Answers

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.

like image 134
Bruno Grieder Avatar answered Sep 22 '22 21:09

Bruno Grieder


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

like image 45
RandomX Avatar answered Sep 25 '22 21:09

RandomX