Skip to content

Commit

Permalink
Revert "[node] Add generics to static members of EventEmitter" (Def…
Browse files Browse the repository at this point in the history
  • Loading branch information
gabritto authored Oct 31, 2024
1 parent 111ed01 commit ab0d6da
Show file tree
Hide file tree
Showing 24 changed files with 350 additions and 1,324 deletions.
12 changes: 12 additions & 0 deletions types/imap/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,18 @@ declare namespace Connection {
declare class Connection extends EventEmitter implements Connection.MessageFunctions {
constructor(config: Connection.Config);

// from NodeJS.EventEmitter
addListener(event: string, listener: Function): this;
on(event: string, listener: Function): this;
once(event: string, listener: Function): this;
removeListener(event: string, listener: Function): this;
removeAllListeners(event?: string): this;
setMaxListeners(n: number): this;
getMaxListeners(): number;
listeners(event: string): Function[];
emit(event: string, ...args: any[]): boolean;
listenerCount(type: string): number;

// from MessageFunctions
/** Searches the currently open mailbox for messages using given criteria. criteria is a list describing what you want to find. For criteria types that require arguments, use an array instead of just the string criteria type name (e.g. ['FROM', '[email protected]']). Prefix criteria types with an "!" to negate. */
search(criteria: any[], callback: (error: Error, uids: number[]) => void): void;
Expand Down
10 changes: 10 additions & 0 deletions types/jake/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ declare global {
*/
reenable(): void;

addListener(event: string, listener: Function): this;
on(event: string, listener: Function): this;
once(event: string, listener: Function): this;
removeListener(event: string, listener: Function): this;
removeAllListeners(event?: string): this;
setMaxListeners(n: number): this;
getMaxListeners(): number;
listeners(event: string): Function[];
emit(event: string, ...args: any[]): boolean;
listenerCount(type: string): number;
complete(value?: any): void;
value: any;

Expand Down
2 changes: 1 addition & 1 deletion types/newman/newman-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const workingDir = "path/to/working/directory";
const insecureFileRead = true;
const requestAgent = new http.Agent();

// $ExpectType EventEmitter<{}>
// $ExpectType EventEmitter<DefaultEventMap>
run(
{
collection,
Expand Down
2 changes: 1 addition & 1 deletion types/node-red/node-red-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async function REDTests() {
// $ExpectType Util
RED.util;

// $ExpectType EventEmitter<{}>
// $ExpectType EventEmitter<DefaultEventMap>
RED.events;

// $ExpectType Hooks
Expand Down
213 changes: 35 additions & 178 deletions types/node/events.d.ts

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions types/node/test/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ declare const any: any;
result = emitter.getMaxListeners();
result = emitter.listenerCount(event);

const handler = () => {};
const handler: Function = () => {};
result = emitter.listenerCount(event, handler);
}

Expand Down Expand Up @@ -141,7 +141,7 @@ async function testEventTarget() {
captureRejectionSymbol2 = events.captureRejectionSymbol;

const emitter = new events.EventEmitter();
emitter[events.captureRejectionSymbol] = (err: Error, name: string | symbol, ...args: any[]) => {};
emitter[events.captureRejectionSymbol] = (err: Error, name: string, ...args: any[]) => {};
}

{
Expand Down Expand Up @@ -193,16 +193,16 @@ async function testEventTarget() {

{
class MyEmitter extends events.EventEmitter {
addListener(event: string | symbol, listener: () => void): this {
addListener(event: string, listener: () => void): this {
return this;
}
listeners(event: string | symbol): Array<() => void> {
listeners(event: string): Array<() => void> {
return [];
}
emit(event: string | symbol, ...args: any[]): boolean {
emit(event: string, ...args: any[]): boolean {
return true;
}
listenerCount(type: string | symbol): number {
listenerCount(type: string): number {
return 0;
}
}
Expand Down
93 changes: 13 additions & 80 deletions types/node/test/events_generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,13 @@ declare const event5: "event5";
}

{
let result1: Promise<T["event1"]>;
let result2: Promise<T["event2"]>;
let result3: Promise<T["event3"]>;
let result4: Promise<T["event4"]>;
let result5: Promise<T["event5"]>;

result1 = events.once(emitter, event1);
result2 = events.once(emitter, event2);
result3 = events.once(emitter, event3);
result4 = events.once(emitter, event4);
result5 = events.once(emitter, event5);
let result: Promise<number[]>;

result = events.once(emitter, event1);
result = events.once(emitter, event2);
result = events.once(emitter, event3);
result = events.once(emitter, event4);
result = events.once(emitter, event5);

emitter.emit("event1", "hello", 42);
emitter.emit("event2", true);
Expand Down Expand Up @@ -150,7 +146,7 @@ declare const event5: "event5";
}

{
let result: Array<keyof T | keyof events.EventEmitterBuiltInEventMap>;
let result: Array<keyof T>;

result = emitter.eventNames();
}
Expand All @@ -161,12 +157,12 @@ declare const event5: "event5";
)
: never;

function on1<K extends keyof T>(event: K, listener: Listener<K>): void {
function on1<K>(event: K, listener: Listener<K>): void {
emitter.on(event, listener);
}

// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
function on2<K extends keyof T>(...args: Parameters<typeof emitter.on<K>>): void {
function on2<K>(...args: Parameters<typeof emitter.on<K>>): void {
emitter.on(...args);
}

Expand Down Expand Up @@ -208,6 +204,7 @@ declare const event5: "event5";
emitter.emit("abc", "hello", 123);
// @ts-expect-error
emitter.emit("abc", 123, false);
// @ts-expect-error
emitter.emit("def", "hello", 123);
}

Expand All @@ -229,6 +226,7 @@ declare const event5: "event5";
emitter.emit(s1, "hello", 123);
// @ts-expect-error
emitter.emit(s1, 123, false);
// @ts-expect-error
emitter.emit(s2, "hello", 123);
}

Expand All @@ -246,71 +244,6 @@ declare const event5: "event5";
emitter.emit(789, 123, false);
// @ts-expect-error
emitter.emit(s1, "hello", false);
emitter.emit(s2, "hello", false);
}

{
const promise1: Promise<[string, number]> = events.once(new events.EventEmitter<T>(), "event1");
const promise2: Promise<[boolean]> = events.once(new events.EventEmitter<T>(), "event2");
const promise3: Promise<[]> = events.once(new events.EventEmitter<T>(), "event3");
const promise4: Promise<string[]> = events.once(new events.EventEmitter<T>(), "event4");
const promise5: Promise<unknown[]> = events.once(new events.EventEmitter<T>(), "event5");
// @ts-expect-error
const promise6: Promise<[string, string]> = events.once(new events.EventEmitter<T>(), "event1");
const promise7: Promise<any[]> = events.once(new events.EventEmitter<T>(), "event");

const iterable1: NodeJS.AsyncIterator<[string, number]> = events.on(new events.EventEmitter<T>(), "event1");
const iterable2: NodeJS.AsyncIterator<[boolean]> = events.on(new events.EventEmitter<T>(), "event2");
const iterable3: NodeJS.AsyncIterator<[]> = events.on(new events.EventEmitter<T>(), "event3");
const iterable4: NodeJS.AsyncIterator<string[]> = events.on(new events.EventEmitter<T>(), "event4");
const iterable5: NodeJS.AsyncIterator<unknown[]> = events.on(new events.EventEmitter<T>(), "event5");
// @ts-expect-error
const iterable6: NodeJS.AsyncIterator<[string, string]> = events.on(new events.EventEmitter<T>(), "event1");
const iterable7: NodeJS.AsyncIterator<any[]> = events.on(new events.EventEmitter<T>(), "event");
}

{
function acceptsEventEmitterInterface(eventEmitter: NodeJS.EventEmitter) {
}

function acceptsEventEmitterClass(eventEmitter: events.EventEmitter) {
}

acceptsEventEmitterInterface(emitter);
acceptsEventEmitterClass(emitter);
}

{
class Extended extends events.EventEmitter<T> {}

class DoubleExtension extends Extended {}

const extended = new Extended();
const doubleExtended = new DoubleExtension();

events.on(extended, "event1"); // $ExpectType AsyncIterator<[string, number], any, any>
events.once(extended, "event1"); // $ExpectType Promise<[string, number]>

events.on(extended, "unknown"); // $ExpectType AsyncIterator<any[], any, any>
events.once(extended, "unknown"); // $ExpectType Promise<any[]>

events.on(doubleExtended, "event1"); // $ExpectType AsyncIterator<[string, number], any, any>
events.once(doubleExtended, "event1"); // $ExpectType Promise<[string, number]>

events.on(doubleExtended, "unknown"); // $ExpectType AsyncIterator<any[], any, any>
events.once(doubleExtended, "unknown"); // $ExpectType Promise<any[]>

extended.addListener(event1, (a: string, b: number | boolean): number => 1);
doubleExtended.addListener(event1, (a: string, b: number | boolean): number => 1);
// @ts-expect-error
extended.addListener(event1, (a: string, b: boolean): number => 1);
// @ts-expect-error
doubleExtended.addListener(event1, (a: string, b: boolean): number => 1);

extended.emit("event1", "hello", 42);
doubleExtended.emit("event1", "hello", 42);
// @ts-expect-error
extended.emit("event1", 123);
// @ts-expect-error
doubleExtended.emit("event1", 123);
emitter.emit(s2, "hello", false);
}
Loading

0 comments on commit ab0d6da

Please sign in to comment.