I am currently working on enhancing the type definitions for a simple npm module called emitter20. The source code of this module spans 20 lines and looks like this:
module.exports = function() {
var subscribers = []
return {
on: function (eventName, cb) {
subscribers.push({
eventName: eventName,
cb: cb
})
},
trigger: function (eventName, data) {
subscribers
.filter(function (subscriber) {
return subscriber.eventName === eventName
})
.forEach(function (subscriber) {
subscriber.cb(data)
})
}
}
}
Within the emitter20 project root, there is an index.d.ts
file structured as follows:
declare module 'emitter20' {
interface IEmitter {
on: (eventName: string, cb: (data?: any) => void) => void;
trigger: (eventName: string, data?: any) => void;
}
interface EmitterFactory {
new(): IEmitter;
}
export = IEmitter;
}
In my attempts to refine the type definitions, I have also experimented with the following approach:
declare module 'emitter20' {
export interface IEmitter {
on: (eventName: string, cb: (data?: any) => void) => void;
trigger: (eventName: string, data?: any) => void;
}
export interface EmitterFactory {
new(): IEmitter;
}
}
When trying to import this module into my project using the code snippet below:
import IEmitter = require('emitter20')
export interface SwapManager extends IEmitter {
manager: any;
}
I encountered the following error message:
error TS2656: Exported external package typings file './node_modules/emitter20/index.d.ts' is not a module. Please contact the package author to update the package definition.
How can I properly define and import the type definition for the emitter20
module?
(On a side note: Typescript imports/exports... definitely a challenging aspect to grasp!)