Skip to content

Commit 42984ae

Browse files
committed
Document TypedEventEmitter workaround for EventEmitter<T> subclasses
Fixes #54
1 parent 670a702 commit 42984ae

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

index.d.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,19 +178,26 @@ export type IteratorMultiArgumentsOptions<EmittedType extends unknown[]> = {
178178
} & IteratorOptions<EmittedType>;
179179

180180
/**
181-
Generic event emitter type for users to cast their emitters to get type inference.
181+
A TypeScript helper type for getting typed event inference with `pEvent`. Cast your emitter to `TypedEventEmitter<EventMap>` to get the resolved type inferred from your event map.
182+
183+
Due to a [TypeScript limitation](https://github.com/microsoft/TypeScript/issues/53750), `pEvent` cannot automatically infer event types from `EventEmitter<T>` subclasses. Use `TypedEventEmitter` as a workaround.
182184
183185
@example
184186
```
185187
import {pEvent, type TypedEventEmitter} from 'p-event';
188+
import {EventEmitter} from 'node:events';
186189
187190
type MyEvents = {
188191
data: [buffer: Uint8Array];
189192
error: [error: Error];
190193
};
191194
192-
const emitter = getEmitter() as TypedEventEmitter<MyEvents>;
193-
const buffer = await pEvent(emitter, 'data'); // Inferred as Uint8Array
195+
class MyEmitter extends EventEmitter<MyEvents> {}
196+
197+
const emitter = new MyEmitter() as unknown as TypedEventEmitter<MyEvents>;
198+
199+
const buffer = await pEvent(emitter, 'data');
200+
//=> Uint8Array
194201
```
195202
*/
196203
export type TypedEventEmitter<EventMap extends Record<string | symbol, unknown[]>> = {

readme.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,30 @@ Default: `[]`
254254

255255
Events that will end the iterator.
256256

257+
### TypedEventEmitter
258+
259+
A TypeScript helper type for getting typed event inference with `pEvent`. Cast your emitter to `TypedEventEmitter<EventMap>` to get the resolved type inferred from your event map.
260+
261+
> [!NOTE]
262+
> Due to a [TypeScript limitation](https://github.com/microsoft/TypeScript/issues/53750), `pEvent` cannot automatically infer event types from `EventEmitter<T>` subclasses. Use `TypedEventEmitter` as a workaround.
263+
264+
```ts
265+
import {pEvent, type TypedEventEmitter} from 'p-event';
266+
import {EventEmitter} from 'node:events';
267+
268+
type MyEvents = {
269+
data: [buffer: Uint8Array];
270+
error: [error: Error];
271+
};
272+
273+
class MyEmitter extends EventEmitter<MyEvents> {}
274+
275+
const emitter = new MyEmitter() as unknown as TypedEventEmitter<MyEvents>;
276+
277+
const buffer = await pEvent(emitter, 'data');
278+
//=> Uint8Array
279+
```
280+
257281
### TimeoutError
258282

259283
Exposed for instance checking and sub-classing.

0 commit comments

Comments
 (0)