15

The direct interface to my library is captured by

index.d.ts

which is currently auto-generated from

index.ts

in my package.json file for my project, typings/types properties point to this index.d.ts file, which is to my understanding, how it should be. All good so far.

However, there are some more types that I would like to export from index.d.ts that are not currently expressed in index.d.ts.

For example I have some manually created types in

foo.d.ts
bar.d.ts

and I want these to be expressed in index.d.ts.

The only way I think that might work, is to import the foo.d.ts/bar.d.ts types into index.d.ts and then in turn export them from that file.

However, I am having trouble with that!

Here is what I tried:

// index.ts

import {IBar} from '../bar.d.ts'
import {IFoo} from '../foo.d.ts'

// ... do some stuff with index.ts yadda yadda

export type IBar = IBar; // doesn't seem to work
export interface IFoo = IFoo; // doesn't seem to work

is this possible? How do I do this?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817

4 Answers4

31

You can export them directly if you don't need to use them.

export * from '../foo/bar';

Or if you need to export only some of them you can use.

export { IFoo, IBar} from '../foo/bar';

Or if you want to rename them during export you can do:

export { IFoo as IOther, IBar as Dogs } from '../foo/bar';

You can read more about them in the MDN documentation here.

Nick Bartlett
  • 4,865
  • 2
  • 24
  • 37
toskv
  • 30,680
  • 7
  • 72
  • 74
  • 1
    This wasn't working for me, ultimately it turned out to be because of using default types. like if `IFoo` is defined like `export default class IFoo { ..... }`, then you index.ts needs to do `export {default as IFoo} from '../foo/bar';` – Kip Feb 13 '22 at 04:06
4

I think the other answers will work. If you do need to use IFoo and IBar in the file, then this is what to do:

import * as IFooImport from '../foo.d.ts';

// use IFooImport

export import IFoo = IFooImport.IFoo;

This works, but a little awkward with the "export import" syntax.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
3

You can try:

export {IBar} from '../bar.d.ts'
export {IFoo} from '../foo.d.ts'
Remo H. Jansen
  • 23,172
  • 11
  • 70
  • 93
0

To make this work in easiest way i found you can follow the example of aws-cdk;

the things to check are:

  1. use relative paths for the internal imports amoung the files of your modules (e.g. import {a} from './subdir1/file1') => this is so when transpiled the transpiled file references remain valid under the dist hierarchy
  2. in your index.ts you need one 'export *' line per file; can generate with a find + sed

    // find . -type f -exec echo {} \;  | sed 
    //     's/\(.*\)\.ts/export \* from \"\1\"/'  
    export * from "./subdir1/file1"
    export * from "./subdir1/file2"
    export * from "./subdir2/file1"

this examples shows how to set up package.json ; look at attrs main, module, types, files, build:esm, build:types https://github.com/tomchen/example-typescript-package/blob/main/package.json

Then subsequently in your target code you can do things like


    import * as somenamespace from "@somepublisher/somemodule";
    import { SomeOtherClass } from "@somepublisher/somemodule";
    const x = new somenamespace.SomeClass();
    const y = new SomeOtherClass();

dancl
  • 689
  • 5
  • 13