diff --git a/index.d.ts b/index.d.ts index 31a9eb3..ce96de2 100644 --- a/index.d.ts +++ b/index.d.ts @@ -11,34 +11,35 @@ type DatalessEventNames = { }[keyof EventData]; /** - Emittery is a strictly typed, fully async EventEmitter implementation. Event listeners can be registered with `on` or `once`, and events can be emitted with `emit`. +Emittery is a strictly typed, fully async EventEmitter implementation. Event listeners can be registered with `on` or `once`, and events can be emitted with `emit`. - `Emittery` has a generic `EventData` type that can be provided by users to strongly type the list of events and the data passed to the listeners for those events. Pass an interface of {[eventName]: undefined | }, with all the event names as the keys and the values as the type of the argument passed to listeners if there is one, or `undefined` if there isn't. +`Emittery` has a generic `EventData` type that can be provided by users to strongly type the list of events and the data passed to the listeners for those events. Pass an interface of {[eventName]: undefined | }, with all the event names as the keys and the values as the type of the argument passed to listeners if there is one, or `undefined` if there isn't. - ```ts - import Emittery = require('emittery'); +@example +``` +import Emittery = require('emittery'); - const emitter = new Emittery< - // Pass `{[eventName: ]: undefined | }` as the first type argument for events that pass data to their listeners. - // A value of `undefined` in this map means the event listeners should expect no data, and a type other than `undefined` means the listeners will receive one argument of that type. - { - open: string, - close: undefined - } - >(); +const emitter = new Emittery< + // Pass `{[eventName: ]: undefined | }` as the first type argument for events that pass data to their listeners. + // A value of `undefined` in this map means the event listeners should expect no data, and a type other than `undefined` means the listeners will receive one argument of that type. + { + open: string, + close: undefined + } +>(); - // Typechecks just fine because the data type for the `open` event is `string`. - emitter.emit('open', 'foo\n'); +// Typechecks just fine because the data type for the `open` event is `string`. +emitter.emit('open', 'foo\n'); - // Typechecks just fine because `close` is present but points to undefined in the event data type map. - emitter.emit('close'); +// Typechecks just fine because `close` is present but points to undefined in the event data type map. +emitter.emit('close'); - // TS compilation error because `1` isn't assignable to `string`. - emitter.emit('open', 1); +// TS compilation error because `1` isn't assignable to `string`. +emitter.emit('open', 1); - // TS compilation error because `other` isn't defined in the event data type map. - emitter.emit('other'); - ``` +// TS compilation error because `other` isn't defined in the event data type map. +emitter.emit('other'); +``` */ declare class Emittery< EventData = Record, // When https://github.com/microsoft/TypeScript/issues/1863 ships, we can switch this to have an index signature including Symbols. If you want to use symbol keys right now, you need to pass an interface with those symbol keys explicitly listed. diff --git a/readme.md b/readme.md index d3506a5..2b419b0 100644 --- a/readme.md +++ b/readme.md @@ -121,9 +121,9 @@ const listener = data => console.log(data); await emitter.emit('🦊', 'c'); emitter.off('🦄', listener); emitter.off(['🐶', '🦊'], listener); - await emitter.emit('🦄', 'a'); // nothing happens - await emitter.emit('🐶', 'b'); // nothing happens - await emitter.emit('🦊', 'c'); // nothing happens + await emitter.emit('🦄', 'a'); // Nothing happens + await emitter.emit('🐶', 'b'); // Nothing happens + await emitter.emit('🦊', 'c'); // Nothing happens })(); ``` @@ -148,8 +148,8 @@ emitter.once(['🦄', '🐶']).then(data => { console.log(data); }); -emitter.emit('🦄', '🌈'); // log => '🌈' x2 -emitter.emit('🐶', '🍖'); // nothing happens +emitter.emit('🦄', '🌈'); // Log => '🌈' x2 +emitter.emit('🐶', '🍖'); // Nothing happens ``` #### events(eventName)