I have a small ts library that gets outputed as UMD and I also output the *.d.ts file automatically via tsconfig: "declaration": true
.
The file being exported looks like:
export class Blue {
alert(): void {
console.log('alerted');
}
}
Using exported UMD module declares a window.myLib
variable.
The d.ts file looks like:
export declare class Blue {
alert(): void;
}
Now, either via webpack, or a typescript option that I have not found I would like to also generate in the d.ts file the following line:
export as namespace myLib;
Is there a way to do this? Thanks.
Instead of just:
export class Blue {
alert(): void {
console.log('alerted');
}
}
Try:
class Blue_ {
alert(): void {
console.log('alerted');
}
}
export { Blue_ as Blue };
declare global {
namespace myLib {
let Blue: typeof Blue_;
}
}
This would spit the following declaration file:
declare class Blue_ {
alert(): void;
}
export { Blue_ as Blue };
declare global {
namespace myLib {
let Blue: typeof Blue_;
}
}
Now myLib.Blue
is accessible globally, and also, class Blue
could be imported like import { Blue } from '...'
. Which is equivalent to having export as namespace myLib;
in the declaration file.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With