@types/node 16.18.116 → 16.18.117

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
node v16.18/README.md CHANGED
@@ -8,7 +8,7 @@ This package contains type definitions for node (https://nodejs.org/).
8
8
  Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node/v16.
9
9
 
10
10
  ### Additional Details
11
- * Last updated: Tue, 29 Oct 2024 17:02:26 GMT
11
+ * Last updated: Thu, 31 Oct 2024 05:35:24 GMT
12
12
  * Dependencies: none
13
13
 
14
14
  # Credits
node v16.18/events.d.ts CHANGED
@@ -34,6 +34,7 @@
34
34
  * ```
35
35
  * @see [source](https://github.com/nodejs/node/blob/v16.9.0/lib/events.js)
36
36
  */
37
+
37
38
  declare module "events" {
38
39
  import { AsyncResource, AsyncResourceOptions } from "node:async_hooks";
39
40
 
@@ -56,25 +57,33 @@ declare module "events" {
56
57
  ): any;
57
58
  }
58
59
  interface StaticEventEmitterOptions {
60
+ /**
61
+ * Can be used to cancel awaiting events.
62
+ */
59
63
  signal?: AbortSignal | undefined;
60
64
  }
61
- interface EventEmitter<T extends EventMap<T> = DefaultEventMap> extends NodeJS.EventEmitter<T> {}
62
- type EventMap<T> = Record<keyof T, any[]> | DefaultEventMap;
63
- type DefaultEventMap = [never];
64
- type AnyRest = [...args: any[]];
65
- type Args<K, T> = T extends DefaultEventMap ? AnyRest : (
66
- K extends keyof T ? T[K] : never
67
- );
68
- type Key<K, T> = T extends DefaultEventMap ? string | symbol : K | keyof T;
69
- type Key2<K, T> = T extends DefaultEventMap ? string | symbol : K & keyof T;
70
- type Listener<K, T, F> = T extends DefaultEventMap ? F : (
71
- K extends keyof T ? (
72
- T[K] extends unknown[] ? (...args: T[K]) => void : never
73
- )
74
- : never
75
- );
76
- type Listener1<K, T> = Listener<K, T, (...args: any[]) => void>;
77
- type Listener2<K, T> = Listener<K, T, Function>;
65
+ interface EventEmitter<Events extends EventMap<Events> = {}> extends NodeJS.EventEmitter<Events> {}
66
+ type EventMap<Events> = Record<keyof Events, unknown[]>;
67
+ type Args<Events extends EventMap<Events>, EventName> = EventName extends keyof Events ? (
68
+ | Events[EventName]
69
+ | (EventName extends keyof EventEmitter.EventEmitterBuiltInEventMap
70
+ ? EventEmitter.EventEmitterBuiltInEventMap[EventName]
71
+ : never)
72
+ )
73
+ : (EventName extends keyof EventEmitter.EventEmitterBuiltInEventMap
74
+ ? EventEmitter.EventEmitterBuiltInEventMap[EventName]
75
+ : any[]);
76
+ type EventNames<Events extends EventMap<Events>> = {} extends Events ? (string | symbol)
77
+ : (keyof Events | keyof EventEmitter.EventEmitterBuiltInEventMap);
78
+ type Listener<Events extends EventMap<Events>, EventName> = EventName extends keyof Events ?
79
+ | ((...args: Events[EventName]) => void)
80
+ | (EventName extends keyof EventEmitter.EventEmitterBuiltInEventMap
81
+ ? (...args: EventEmitter.EventEmitterBuiltInEventMap[EventName]) => void
82
+ : never)
83
+ : (EventName extends keyof EventEmitter.EventEmitterBuiltInEventMap
84
+ ? (...args: EventEmitter.EventEmitterBuiltInEventMap[EventName]) => void
85
+ : (...args: any[]) => void);
86
+
78
87
  /**
79
88
  * The `EventEmitter` class is defined and exposed by the `events` module:
80
89
  *
@@ -88,10 +97,20 @@ declare module "events" {
88
97
  * It supports the following option:
89
98
  * @since v0.1.26
90
99
  */
91
- class EventEmitter<T extends EventMap<T> = DefaultEventMap> {
100
+ class EventEmitter<Events extends EventMap<Events> = {}> {
92
101
  constructor(options?: EventEmitterOptions);
93
102
 
94
- [EventEmitter.captureRejectionSymbol]?<K>(error: Error, event: Key<K, T>, ...args: Args<K, T>): void;
103
+ // This "property" is used to brand a specific instance of the EventEmitter class with its Event map, which is needed
104
+ // in order to infer the map if we have a chain like `class A extends EventEmitter<{}>` (or many levels deep)
105
+ // It is also marked as possibly undefined in order to allow something like `const t: NodeJS.EventEmitter<{}> = { <insert implementation here> };`
106
+ readonly #internalTypeOnlyBrand?: Events;
107
+
108
+ [EventEmitter.captureRejectionSymbol]?<EventName extends EventNames<Events>>(
109
+ error: Error,
110
+ event: EventName,
111
+ ...args: Args<Events, EventName>
112
+ ): void;
113
+ [EventEmitter.captureRejectionSymbol]?(error: Error, event: string | symbol, ...args: any[]): void;
95
114
 
96
115
  /**
97
116
  * Creates a `Promise` that is fulfilled when the `EventEmitter` emits the given
@@ -130,7 +149,7 @@ declare module "events" {
130
149
  * run();
131
150
  * ```
132
151
  *
133
- * The special handling of the `'error'` event is only used when `events.once()`is used to wait for another event. If `events.once()` is used to wait for the
152
+ * The special handling of the `'error'` event is only used when `events.once()` is used to wait for another event. If `events.once()` is used to wait for the
134
153
  * '`error'` event itself, then it is treated as any other kind of event without
135
154
  * special handling:
136
155
  *
@@ -141,7 +160,7 @@ declare module "events" {
141
160
  *
142
161
  * once(ee, 'error')
143
162
  * .then(([err]) => console.log('ok', err.message))
144
- * .catch((err) => console.log('error', err.message));
163
+ * .catch((err) => console.error('error', err.message));
145
164
  *
146
165
  * ee.emit('error', new Error('boom'));
147
166
  *
@@ -175,6 +194,11 @@ declare module "events" {
175
194
  * ```
176
195
  * @since v11.13.0, v10.16.0
177
196
  */
197
+ static once<Events extends EventMap<Events>, EventName extends EventNames<Events>>(
198
+ emitter: EventEmitter<Events>,
199
+ eventName: EventName,
200
+ options?: StaticEventEmitterOptions,
201
+ ): Promise<Args<Events, EventName>>;
178
202
  static once(
179
203
  emitter: NodeEventTarget,
180
204
  eventName: string | symbol,
@@ -236,16 +260,20 @@ declare module "events" {
236
260
  * process.nextTick(() => ac.abort());
237
261
  * ```
238
262
  * @since v13.6.0, v12.16.0
239
- * @param eventName The name of the event being listened for
240
- * @return that iterates `eventName` events emitted by the `emitter`
263
+ * @return An `AsyncIterator` that iterates `eventName` events emitted by the `emitter`
241
264
  */
265
+ static on<Events extends EventMap<Events>, EventName extends EventNames<Events>>(
266
+ emitter: EventEmitter<Events>,
267
+ eventName: EventName,
268
+ options?: StaticEventEmitterOptions,
269
+ ): NodeJS.AsyncIterator<Args<Events, EventName>>;
242
270
  static on(
243
271
  emitter: NodeJS.EventEmitter,
244
- eventName: string,
272
+ eventName: string | symbol,
245
273
  options?: StaticEventEmitterOptions,
246
- ): NodeJS.AsyncIterator<any>;
274
+ ): NodeJS.AsyncIterator<any[]>;
247
275
  /**
248
- * A class method that returns the number of listeners for the given `eventName`registered on the given `emitter`.
276
+ * A class method that returns the number of listeners for the given `eventName` registered on the given `emitter`.
249
277
  *
250
278
  * ```js
251
279
  * import { EventEmitter, listenerCount } from 'node:events';
@@ -260,6 +288,27 @@ declare module "events" {
260
288
  * @param emitter The emitter to query
261
289
  * @param eventName The event name
262
290
  */
291
+ static listenerCount<Events extends EventMap<Events>, EventName extends EventNames<Events>>(
292
+ emitter: EventEmitter<Events>,
293
+ eventName: EventName,
294
+ ): number;
295
+ /**
296
+ * A class method that returns the number of listeners for the given `eventName` registered on the given `emitter`.
297
+ *
298
+ * ```js
299
+ * const { EventEmitter, listenerCount } = require('events');
300
+ *
301
+ * const myEmitter = new EventEmitter();
302
+ * myEmitter.on('event', () => {});
303
+ * myEmitter.on('event', () => {});
304
+ * console.log(listenerCount(myEmitter, 'event'));
305
+ * // Prints: 2
306
+ * ```
307
+ * @since v0.9.12
308
+ * @deprecated Since v3.2.0 - Use `listenerCount` instead.
309
+ * @param emitter The emitter to query
310
+ * @param eventName The event name
311
+ */
263
312
  static listenerCount(emitter: NodeJS.EventEmitter, eventName: string | symbol): number;
264
313
  /**
265
314
  * Returns a copy of the array of listeners for the event named `eventName`.
@@ -277,17 +326,21 @@ declare module "events" {
277
326
  * const ee = new EventEmitter();
278
327
  * const listener = () => console.log('Events are fun');
279
328
  * ee.on('foo', listener);
280
- * getEventListeners(ee, 'foo'); // [listener]
329
+ * console.log(getEventListeners(ee, 'foo')); // [ [Function: listener] ]
281
330
  * }
282
331
  * {
283
332
  * const et = new EventTarget();
284
333
  * const listener = () => console.log('Events are fun');
285
334
  * et.addEventListener('foo', listener);
286
- * getEventListeners(et, 'foo'); // [listener]
335
+ * console.log(getEventListeners(et, 'foo')); // [ [Function: listener] ]
287
336
  * }
288
337
  * ```
289
- * @since v15.2.0
338
+ * @since v15.2.0, v14.17.0
290
339
  */
340
+ static getEventListeners<Events extends EventMap<Events>, EventName extends EventNames<Events>>(
341
+ emitter: EventEmitter<Events>,
342
+ name: EventName,
343
+ ): Array<Listener<Events, EventName>>;
291
344
  static getEventListeners(emitter: DOMEventTarget | NodeJS.EventEmitter, name: string | symbol): Function[];
292
345
  /**
293
346
  * ```js
@@ -308,21 +361,65 @@ declare module "events" {
308
361
  */
309
362
  static setMaxListeners(n?: number, ...eventTargets: Array<DOMEventTarget | NodeJS.EventEmitter>): void;
310
363
  /**
311
- * This symbol shall be used to install a listener for only monitoring `'error'`
312
- * events. Listeners installed using this symbol are called before the regular
313
- * `'error'` listeners are called.
364
+ * This symbol shall be used to install a listener for only monitoring `'error'` events. Listeners installed using this symbol are called before the regular `'error'` listeners are called.
314
365
  *
315
- * Installing a listener using this symbol does not change the behavior once an
316
- * `'error'` event is emitted, therefore the process will still crash if no
366
+ * Installing a listener using this symbol does not change the behavior once an `'error'` event is emitted. Therefore, the process will still crash if no
317
367
  * regular `'error'` listener is installed.
368
+ * @since v13.6.0, v12.17.0
318
369
  */
319
370
  static readonly errorMonitor: unique symbol;
371
+ /**
372
+ * Value: `Symbol.for('nodejs.rejection')`
373
+ *
374
+ * See how to write a custom `rejection handler`.
375
+ * @since v13.4.0, v12.16.0
376
+ */
320
377
  static readonly captureRejectionSymbol: unique symbol;
321
378
  /**
322
- * Sets or gets the default captureRejection value for all emitters.
379
+ * Value: [boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
380
+ *
381
+ * Change the default `captureRejections` option on all new `EventEmitter` objects.
382
+ * @since v13.4.0, v12.16.0
323
383
  */
324
- // TODO: These should be described using static getter/setter pairs:
325
384
  static captureRejections: boolean;
385
+ /**
386
+ * By default, a maximum of `10` listeners can be registered for any single
387
+ * event. This limit can be changed for individual `EventEmitter` instances
388
+ * using the `emitter.setMaxListeners(n)` method. To change the default
389
+ * for _all_`EventEmitter` instances, the `events.defaultMaxListeners` property
390
+ * can be used. If this value is not a positive number, a `RangeError` is thrown.
391
+ *
392
+ * Take caution when setting the `events.defaultMaxListeners` because the
393
+ * change affects _all_ `EventEmitter` instances, including those created before
394
+ * the change is made. However, calling `emitter.setMaxListeners(n)` still has
395
+ * precedence over `events.defaultMaxListeners`.
396
+ *
397
+ * This is not a hard limit. The `EventEmitter` instance will allow
398
+ * more listeners to be added but will output a trace warning to stderr indicating
399
+ * that a "possible EventEmitter memory leak" has been detected. For any single
400
+ * `EventEmitter`, the `emitter.getMaxListeners()` and `emitter.setMaxListeners()` methods can be used to
401
+ * temporarily avoid this warning:
402
+ *
403
+ * ```js
404
+ * const EventEmitter = require('events');
405
+ * const emitter = new EventEmitter();
406
+ * emitter.setMaxListeners(emitter.getMaxListeners() + 1);
407
+ * emitter.once('event', () => {
408
+ * // do stuff
409
+ * emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));
410
+ * });
411
+ * ```
412
+ *
413
+ * The `--trace-warnings` command-line flag can be used to display the
414
+ * stack trace for such warnings.
415
+ *
416
+ * The emitted warning can be inspected with `process.on('warning')` and will
417
+ * have the additional `emitter`, `type`, and `count` properties, referring to
418
+ * the event emitter instance, the event's name and the number of attached
419
+ * listeners, respectively.
420
+ * Its `name` property is set to `'MaxListenersExceededWarning'`.
421
+ * @since v0.11.2
422
+ */
326
423
  static defaultMaxListeners: number;
327
424
  }
328
425
  import internal = require("node:events");
@@ -350,13 +447,41 @@ declare module "events" {
350
447
  }
351
448
 
352
449
  /**
353
- * Integrates `EventEmitter` with `AsyncResource` for `EventEmitter`s that require
354
- * manual async tracking. Specifically, all events emitted by instances of
355
- * `EventEmitterAsyncResource` will run within its async context.
450
+ * Integrates `EventEmitter` with `AsyncResource` for `EventEmitter`s that
451
+ * require manual async tracking. Specifically, all events emitted by instances
452
+ * of `events.EventEmitterAsyncResource` will run within its `async context`.
356
453
  *
357
- * The EventEmitterAsyncResource class has the same methods and takes the
358
- * same options as EventEmitter and AsyncResource themselves.
359
- * @throws if `options.name` is not provided when instantiated directly.
454
+ * ```js
455
+ * const { EventEmitterAsyncResource, EventEmitter } = require('events');
456
+ * const { notStrictEqual, strictEqual } = require('assert');
457
+ * const { executionAsyncId, triggerAsyncId } = require('async_hooks');
458
+ *
459
+ * // Async tracking tooling will identify this as 'Q'.
460
+ * const ee1 = new EventEmitterAsyncResource({ name: 'Q' });
461
+ *
462
+ * // 'foo' listeners will run in the EventEmitters async context.
463
+ * ee1.on('foo', () => {
464
+ * strictEqual(executionAsyncId(), ee1.asyncId);
465
+ * strictEqual(triggerAsyncId(), ee1.triggerAsyncId);
466
+ * });
467
+ *
468
+ * const ee2 = new EventEmitter();
469
+ *
470
+ * // 'foo' listeners on ordinary EventEmitters that do not track async
471
+ * // context, however, run in the same async context as the emit().
472
+ * ee2.on('foo', () => {
473
+ * notStrictEqual(executionAsyncId(), ee2.asyncId);
474
+ * notStrictEqual(triggerAsyncId(), ee2.triggerAsyncId);
475
+ * });
476
+ *
477
+ * Promise.resolve().then(() => {
478
+ * ee1.emit('foo');
479
+ * ee2.emit('foo');
480
+ * });
481
+ * ```
482
+ *
483
+ * The `EventEmitterAsyncResource` class has the same methods and takes the
484
+ * same options as `EventEmitter` and `AsyncResource` themselves.
360
485
  * @since v17.4.0, v16.14.0
361
486
  */
362
487
  export class EventEmitterAsyncResource extends EventEmitter {
@@ -365,34 +490,62 @@ declare module "events" {
365
490
  */
366
491
  constructor(options?: EventEmitterAsyncResourceOptions);
367
492
  /**
368
- * Call all destroy hooks. This should only ever be called once. An
369
- * error will be thrown if it is called more than once. This must be
370
- * manually called. If the resource is left to be collected by the GC then
371
- * the destroy hooks will never be called.
493
+ * Call all `destroy` hooks. This should only ever be called once. An error will
494
+ * be thrown if it is called more than once. This **must** be manually called. If
495
+ * the resource is left to be collected by the GC then the `destroy` hooks will
496
+ * never be called.
372
497
  */
373
498
  emitDestroy(): void;
374
- /** The unique asyncId assigned to the resource. */
499
+ /**
500
+ * The unique `asyncId` assigned to the resource.
501
+ */
375
502
  readonly asyncId: number;
376
- /** The same triggerAsyncId that is passed to the AsyncResource constructor. */
503
+ /**
504
+ * The same triggerAsyncId that is passed to the AsyncResource constructor.
505
+ */
377
506
  readonly triggerAsyncId: number;
378
- /** The underlying AsyncResource */
507
+ /**
508
+ * The returned `AsyncResource` object has an additional `eventEmitter` property
509
+ * that provides a reference to this `EventEmitterAsyncResource`.
510
+ */
379
511
  readonly asyncResource: EventEmitterReferencingAsyncResource;
380
512
  }
513
+
514
+ export interface EventEmitterBuiltInEventMap {
515
+ newListener: [eventName: string | symbol, listener: Function];
516
+ removeListener: [eventName: string | symbol, listener: Function];
517
+ }
381
518
  }
382
519
  global {
383
520
  namespace NodeJS {
384
- interface EventEmitter<T extends EventMap<T> = DefaultEventMap> {
385
- [EventEmitter.captureRejectionSymbol]?<K>(error: Error, event: Key<K, T>, ...args: Args<K, T>): void;
521
+ interface EventEmitter<Events extends EventMap<Events> = {}> {
522
+ [EventEmitter.captureRejectionSymbol]?<EventName extends EventNames<Events>>(
523
+ error: Error,
524
+ event: EventName,
525
+ ...args: Args<Events, EventName>
526
+ ): void;
527
+ [EventEmitter.captureRejectionSymbol]?<EventName extends string | symbol>(
528
+ error: Error,
529
+ event: EventName,
530
+ ...args: Args<Events, EventName>
531
+ ): void;
386
532
  /**
387
533
  * Alias for `emitter.on(eventName, listener)`.
388
534
  * @since v0.1.26
389
535
  */
390
- addListener<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
536
+ addListener<EventName extends EventNames<Events>>(
537
+ eventName: EventName,
538
+ listener: Listener<Events, EventName>,
539
+ ): this;
540
+ addListener<EventName extends string | symbol>(
541
+ eventName: EventName,
542
+ listener: Listener<Events, EventName>,
543
+ ): this;
391
544
  /**
392
- * Adds the `listener` function to the end of the listeners array for the
393
- * event named `eventName`. No checks are made to see if the `listener` has
394
- * already been added. Multiple calls passing the same combination of `eventName` and `listener` will result in the `listener` being added, and called, multiple
395
- * times.
545
+ * Adds the `listener` function to the end of the listeners array for the event
546
+ * named `eventName`. No checks are made to see if the `listener` has already
547
+ * been added. Multiple calls passing the same combination of `eventName` and
548
+ * `listener` will result in the `listener` being added, and called, multiple times.
396
549
  *
397
550
  * ```js
398
551
  * server.on('connection', (stream) => {
@@ -402,10 +555,11 @@ declare module "events" {
402
555
  *
403
556
  * Returns a reference to the `EventEmitter`, so that calls can be chained.
404
557
  *
405
- * By default, event listeners are invoked in the order they are added. The`emitter.prependListener()` method can be used as an alternative to add the
558
+ * By default, event listeners are invoked in the order they are added. The `emitter.prependListener()` method can be used as an alternative to add the
406
559
  * event listener to the beginning of the listeners array.
407
560
  *
408
561
  * ```js
562
+ * const EventEmitter = require('events');
409
563
  * const myEE = new EventEmitter();
410
564
  * myEE.on('foo', () => console.log('a'));
411
565
  * myEE.prependListener('foo', () => console.log('b'));
@@ -418,9 +572,16 @@ declare module "events" {
418
572
  * @param eventName The name of the event.
419
573
  * @param listener The callback function
420
574
  */
421
- on<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
575
+ on<EventName extends EventNames<Events>>(
576
+ eventName: EventName,
577
+ listener: Listener<Events, EventName>,
578
+ ): this;
579
+ on<EventName extends string | symbol>(
580
+ eventName: EventName,
581
+ listener: Listener<Events, EventName>,
582
+ ): this;
422
583
  /**
423
- * Adds a **one-time**`listener` function for the event named `eventName`. The
584
+ * Adds a **one-time** `listener` function for the event named `eventName`. The
424
585
  * next time `eventName` is triggered, this listener is removed and then invoked.
425
586
  *
426
587
  * ```js
@@ -431,10 +592,11 @@ declare module "events" {
431
592
  *
432
593
  * Returns a reference to the `EventEmitter`, so that calls can be chained.
433
594
  *
434
- * By default, event listeners are invoked in the order they are added. The`emitter.prependOnceListener()` method can be used as an alternative to add the
595
+ * By default, event listeners are invoked in the order they are added. The `emitter.prependOnceListener()` method can be used as an alternative to add the
435
596
  * event listener to the beginning of the listeners array.
436
597
  *
437
598
  * ```js
599
+ * const EventEmitter = require('events');
438
600
  * const myEE = new EventEmitter();
439
601
  * myEE.once('foo', () => console.log('a'));
440
602
  * myEE.prependOnceListener('foo', () => console.log('b'));
@@ -447,9 +609,16 @@ declare module "events" {
447
609
  * @param eventName The name of the event.
448
610
  * @param listener The callback function
449
611
  */
450
- once<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
612
+ once<EventName extends EventNames<Events>>(
613
+ eventName: EventName,
614
+ listener: Listener<Events, EventName>,
615
+ ): this;
616
+ once<EventName extends string | symbol>(
617
+ eventName: EventName,
618
+ listener: Listener<Events, EventName>,
619
+ ): this;
451
620
  /**
452
- * Removes the specified `listener` from the listener array for the event named`eventName`.
621
+ * Removes the specified `listener` from the listener array for the event named `eventName`.
453
622
  *
454
623
  * ```js
455
624
  * const callback = (stream) => {
@@ -466,10 +635,12 @@ declare module "events" {
466
635
  * called multiple times to remove each instance.
467
636
  *
468
637
  * Once an event is emitted, all listeners attached to it at the
469
- * time of emitting are called in order. This implies that any`removeListener()` or `removeAllListeners()` calls _after_ emitting and_before_ the last listener finishes execution will
470
- * not remove them from`emit()` in progress. Subsequent events behave as expected.
638
+ * time of emitting are called in order. This implies that any `removeListener()` or `removeAllListeners()` calls _after_ emitting and _before_ the last listener finishes execution
639
+ * will not remove them from`emit()` in progress. Subsequent events behave as expected.
471
640
  *
472
641
  * ```js
642
+ * const EventEmitter = require('events');
643
+ * class MyEmitter extends EventEmitter {}
473
644
  * const myEmitter = new MyEmitter();
474
645
  *
475
646
  * const callbackA = () => {
@@ -507,9 +678,10 @@ declare module "events" {
507
678
  *
508
679
  * When a single function has been added as a handler multiple times for a single
509
680
  * event (as in the example below), `removeListener()` will remove the most
510
- * recently added instance. In the example the `once('ping')`listener is removed:
681
+ * recently added instance. In the example the `once('ping')` listener is removed:
511
682
  *
512
683
  * ```js
684
+ * const EventEmitter = require('events');
513
685
  * const ee = new EventEmitter();
514
686
  *
515
687
  * function pong() {
@@ -527,12 +699,26 @@ declare module "events" {
527
699
  * Returns a reference to the `EventEmitter`, so that calls can be chained.
528
700
  * @since v0.1.26
529
701
  */
530
- removeListener<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
702
+ removeListener<EventName extends EventNames<Events>>(
703
+ eventName: EventName,
704
+ listener: Listener<Events, EventName>,
705
+ ): this;
706
+ removeListener<EventName extends string | symbol>(
707
+ eventName: EventName,
708
+ listener: Listener<Events, EventName>,
709
+ ): this;
531
710
  /**
532
711
  * Alias for `emitter.removeListener()`.
533
712
  * @since v10.0.0
534
713
  */
535
- off<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
714
+ off<EventName extends EventNames<Events>>(
715
+ eventName: EventName,
716
+ listener: Listener<Events, EventName>,
717
+ ): this;
718
+ off<EventName extends string | symbol>(
719
+ eventName: EventName,
720
+ listener: Listener<Events, EventName>,
721
+ ): this;
536
722
  /**
537
723
  * Removes all listeners, or those of the specified `eventName`.
538
724
  *
@@ -543,12 +729,15 @@ declare module "events" {
543
729
  * Returns a reference to the `EventEmitter`, so that calls can be chained.
544
730
  * @since v0.1.26
545
731
  */
546
- removeAllListeners(event?: Key<unknown, T>): this;
732
+ /* eslint-disable @definitelytyped/no-unnecessary-generics */
733
+ removeAllListeners<EventName extends EventNames<Events>>(eventName: EventName): this;
734
+ removeAllListeners<EventName extends string | symbol>(eventName?: EventName): this;
735
+ /* eslint-enable @definitelytyped/no-unnecessary-generics */
547
736
  /**
548
737
  * By default `EventEmitter`s will print a warning if more than `10` listeners are
549
738
  * added for a particular event. This is a useful default that helps finding
550
739
  * memory leaks. The `emitter.setMaxListeners()` method allows the limit to be
551
- * modified for this specific `EventEmitter` instance. The value can be set to`Infinity` (or `0`) to indicate an unlimited number of listeners.
740
+ * modified for this specific `EventEmitter` instance. The value can be set to `Infinity` (or `0`) to indicate an unlimited number of listeners.
552
741
  *
553
742
  * Returns a reference to the `EventEmitter`, so that calls can be chained.
554
743
  * @since v0.3.5
@@ -572,12 +761,18 @@ declare module "events" {
572
761
  * ```
573
762
  * @since v0.1.26
574
763
  */
575
- listeners<K>(eventName: Key<K, T>): Array<Listener2<K, T>>;
764
+ listeners<EventName extends EventNames<Events>>(
765
+ eventName: EventName,
766
+ ): Array<Listener<Events, EventName>>;
767
+ listeners<EventName extends string | symbol>(
768
+ eventName: EventName,
769
+ ): Array<Listener<Events, EventName>>;
576
770
  /**
577
771
  * Returns a copy of the array of listeners for the event named `eventName`,
578
772
  * including any wrappers (such as those created by `.once()`).
579
773
  *
580
774
  * ```js
775
+ * const EventEmitter = require('events');
581
776
  * const emitter = new EventEmitter();
582
777
  * emitter.once('log', () => console.log('log once'));
583
778
  *
@@ -602,9 +797,14 @@ declare module "events" {
602
797
  * ```
603
798
  * @since v9.4.0
604
799
  */
605
- rawListeners<K>(eventName: Key<K, T>): Array<Listener2<K, T>>;
800
+ rawListeners<EventName extends EventNames<Events>>(
801
+ eventName: EventName,
802
+ ): Array<Listener<Events, EventName>>;
803
+ rawListeners<EventName extends string | symbol>(
804
+ eventName: EventName,
805
+ ): Array<Listener<Events, EventName>>;
606
806
  /**
607
- * Synchronously calls each of the listeners registered for the event named`eventName`, in the order they were registered, passing the supplied arguments
807
+ * Synchronously calls each of the listeners registered for the event named `eventName`, in the order they were registered, passing the supplied arguments
608
808
  * to each.
609
809
  *
610
810
  * Returns `true` if the event had listeners, `false` otherwise.
@@ -643,18 +843,35 @@ declare module "events" {
643
843
  * ```
644
844
  * @since v0.1.26
645
845
  */
646
- emit<K>(eventName: Key<K, T>, ...args: Args<K, T>): boolean;
846
+ emit<EventName extends EventNames<Events>>(
847
+ eventName: EventName,
848
+ ...args: Args<Events, EventName>
849
+ ): boolean;
850
+ emit<EventName extends string | symbol>(
851
+ eventName: EventName,
852
+ ...args: Args<Events, EventName>
853
+ ): boolean;
647
854
  /**
648
- * Returns the number of listeners listening to the event named `eventName`.
855
+ * Returns the number of listeners listening for the event named `eventName`.
856
+ * If `listener` is provided, it will return how many times the listener is found
857
+ * in the list of the listeners of the event.
649
858
  * @since v3.2.0
650
859
  * @param eventName The name of the event being listened for
860
+ * @param listener The event handler function
651
861
  */
652
- listenerCount<K>(eventName: Key<K, T>, listener?: Listener2<K, T>): number;
862
+ listenerCount<EventName extends EventNames<Events>>(
863
+ eventName: EventName,
864
+ listener?: Listener<Events, EventName>,
865
+ ): number;
866
+ listenerCount<EventName extends string | symbol>(
867
+ eventName: EventName,
868
+ listener?: Listener<Events, EventName>,
869
+ ): number;
653
870
  /**
654
871
  * Adds the `listener` function to the _beginning_ of the listeners array for the
655
872
  * event named `eventName`. No checks are made to see if the `listener` has
656
- * already been added. Multiple calls passing the same combination of `eventName` and `listener` will result in the `listener` being added, and called, multiple
657
- * times.
873
+ * already been added. Multiple calls passing the same combination of `eventName`
874
+ * and `listener` will result in the `listener` being added, and called, multiple times.
658
875
  *
659
876
  * ```js
660
877
  * server.prependListener('connection', (stream) => {
@@ -667,9 +884,16 @@ declare module "events" {
667
884
  * @param eventName The name of the event.
668
885
  * @param listener The callback function
669
886
  */
670
- prependListener<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
887
+ prependListener<EventName extends EventNames<Events>>(
888
+ eventName: EventName,
889
+ listener: Listener<Events, EventName>,
890
+ ): this;
891
+ prependListener<EventName extends string | symbol>(
892
+ eventName: EventName,
893
+ listener: Listener<Events, EventName>,
894
+ ): this;
671
895
  /**
672
- * Adds a **one-time**`listener` function for the event named `eventName` to the_beginning_ of the listeners array. The next time `eventName` is triggered, this
896
+ * Adds a **one-time**`listener` function for the event named `eventName` to the _beginning_ of the listeners array. The next time `eventName` is triggered, this
673
897
  * listener is removed, and then invoked.
674
898
  *
675
899
  * ```js
@@ -683,7 +907,14 @@ declare module "events" {
683
907
  * @param eventName The name of the event.
684
908
  * @param listener The callback function
685
909
  */
686
- prependOnceListener<K>(eventName: Key<K, T>, listener: Listener1<K, T>): this;
910
+ prependOnceListener<EventName extends EventNames<Events>>(
911
+ eventName: EventName,
912
+ listener: Listener<Events, EventName>,
913
+ ): this;
914
+ prependOnceListener<EventName extends string | symbol>(
915
+ eventName: EventName,
916
+ listener: Listener<Events, EventName>,
917
+ ): this;
687
918
  /**
688
919
  * Returns an array listing the events for which the emitter has registered
689
920
  * listeners. The values in the array are strings or `Symbol`s.
@@ -702,7 +933,7 @@ declare module "events" {
702
933
  * ```
703
934
  * @since v6.0.0
704
935
  */
705
- eventNames(): Array<(string | symbol) & Key2<unknown, T>>;
936
+ eventNames(): Array<(string | symbol)> & Array<EventNames<Events>>;
706
937
  }
707
938
  }
708
939
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@types/node",
3
- "version": "16.18.116",
3
+ "version": "16.18.117",
4
4
  "description": "TypeScript definitions for node",
5
5
  "homepage": "https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node",
6
6
  "license": "MIT",
@@ -218,6 +218,6 @@
218
218
  "scripts": {},
219
219
  "dependencies": {},
220
220
  "peerDependencies": {},
221
- "typesPublisherContentHash": "89bf8903de3ba398de8f681a07b47774e7bd8cc7ee302ce637d310f2781cf850",
221
+ "typesPublisherContentHash": "a6d05824c24f34915fd1b47759c0000db6a47d6f893cf8ee18d406f006e28b80",
222
222
  "typeScriptVersion": "4.8"
223
223
  }