You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
182
184
183
185
@example
184
186
```
185
187
import {pEvent, type TypedEventEmitter} from 'p-event';
188
+
import {EventEmitter} from 'node:events';
186
189
187
190
type MyEvents = {
188
191
data: [buffer: Uint8Array];
189
192
error: [error: Error];
190
193
};
191
194
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>;
Copy file name to clipboardExpand all lines: readme.md
+24Lines changed: 24 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -254,6 +254,30 @@ Default: `[]`
254
254
255
255
Events that will end the iterator.
256
256
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.
0 commit comments