Reputation: 8176
I'm have a library which has the following type declarations:
declare namespace Something {
interface Else extends EventEmitter {
on(event: 'some-event', listener: (event: Event) => void): this;
on(event: 'another-event', listener: (event: OtherEvent) => void): this,
// etc
}
const else: Else;
}
declare module 'something' {
export = Something;
}
And its used like:
import { else } from 'something';
else.on('some-event', e => { });
I'm creating a library that extends the above library and emits other events on else
, so I want to extend the above types. The following feels like it should work, but only if I include it in the module that is using new-amazing-event
.
declare namespace Something {
interface Else {
on(event: 'new-amazing-event', listener: (arg: AmazingArg) => void): this,
}
}
How can I ensure that consumers of my library get these extended type declarations without having to include the above?
Upvotes: 2
Views: 362
Reputation: 30909
Just write the augmentation in your library:
declare module "something" {
interface Else {
on(event: 'new-amazing-event', listener: (arg: AmazingArg) => void): this,
}
}
Then you need to make sure that consumers of your library reference the file containing this augmentation when they are compiled. The way to do that will depend on the form in which your library is packaged (e.g., NPM package) and the TypeScript compiler options used by the consumers. If you provide more information, I or others may be able to provide more help.
Upvotes: 2