TypeStrong / dts-bundle

Export TypeScript .d.ts files as an external module definition
MIT License
308 stars 57 forks source link

Reexporting module as named export for module library #61

Open fuzzyfelts opened 6 years ago

fuzzyfelts commented 6 years ago

Hi,

I am trying to use dts-bundle to export a bundle as a module library. I am re-exporting all the exports from another file, and while this works without bundling it doesn't seem to when bundled.

My question is really 2-fold.

1) Is this a gap/bug in the dts-bundle library? 2) What should the bundle.d.ts look like?

This is my minimal example. Consider I am trying to publish an 'animals' npm package. It's very simple consisting of the following.

mammals.ts

export class Monkey { }
export class Lion { }
export class Tiger { }

index.ts

import * as mammals from './mammals';
export { mammals }

I want to use it as follows within some other code in another module somewhere

zoo.ts

import { mammals } from 'animals';
new mammals.Lion();

If you create a bundle this using the command

dts-bundle --name bundle --main index.d.ts --outputAsModuleFolder

You get the file

bundle.d.ts

export { mammals };

export class Monkey {
}
export class Lion {
}
export class Tiger {
}

which leads to a typescript error 'Cannot find name mammals'

The initial d.ts files look like this:-

mammals.d.ts

export declare class Monkey {
}
export declare class Lion {
}
export declare class Tiger {
}

index.d.ts

import * as mammals from './mammals';
export { mammals };
shanehsu commented 5 years ago

Similar edge case,

export { value as anotherName } from './some-file'

Will generate

export const value: number

instead of

export const anotherName: number
im-danwu commented 5 years ago

Hey did you guys find a solution for this?

shanehsu commented 5 years ago

Better off writing something of our own at this point...

im-danwu commented 5 years ago

Hahaha yeah this is my first time working with declaration files so I'm surprised there's no automated way of creating these that are... more robust.

@shanehsu I figured out a workaround that's not too bad using namespaces. You would modify the above example as...

mammals.ts

export namespace mammals {
    export class Monkey { }
    export class Lion { }
    export class Tiger { }
}

index.ts

export { mammals } from './mammals';

This actually feels nicer as we don't need to import * as just to export again