19

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?

4 Answers 4

40

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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';
export * from "..." results in an error with the library where I'm trying to use it module ... has no exported member. However, if I explicitly export the object I'm trying to consume it works fine. export { myThing} from "...". Why is this?
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.

1 Comment

Thanks for the tip! To export everything, I did export import myThings = myThings;
3

You can try:

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

Comments

1

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.