From 415bf654d2124b1c5d9dcd2b291d7b4af68248a1 Mon Sep 17 00:00:00 2001 From: Trim21 Date: Sat, 17 Dec 2022 04:31:09 +0800 Subject: [PATCH 1/2] add known type to on --- lib/Redis.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/Redis.ts b/lib/Redis.ts index cdcabb31..fc013f9f 100644 --- a/lib/Redis.ts +++ b/lib/Redis.ts @@ -838,7 +838,28 @@ class Redis extends Commander implements DataHandledable { } } -interface Redis extends EventEmitter {} +interface Redis extends EventEmitter { + on(event: "message", cb: (channel: string, message: string) => void): this; + + on( + event: "messageBuffer", + cb: (channel: string, message: Buffer) => void + ): this; + + on( + event: "pmessage", + cb: (pattern: string, channel: string, message: string) => void + ): this; + + on( + event: "pmessageBuffer", + cb: (pattern: string, channel: string, message: Buffer) => void + ): this; + + // base method of EventEmitter + on(eventName: string | symbol, listener: (...args: any[]) => void): this; +} + applyMixin(Redis, EventEmitter); addTransactionSupport(Redis.prototype); From eb3e2fd7b40b7d5e331f4aa721fb7213be5c4bfe Mon Sep 17 00:00:00 2001 From: Zihua Li Date: Sat, 15 Apr 2023 17:24:02 +0800 Subject: [PATCH 2/2] Add more types to events --- lib/Redis.ts | 26 ++++++++++++-- test/typing/events.test-.ts | 68 +++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 test/typing/events.test-.ts diff --git a/lib/Redis.ts b/lib/Redis.ts index fc013f9f..ff7cf29b 100644 --- a/lib/Redis.ts +++ b/lib/Redis.ts @@ -840,24 +840,44 @@ class Redis extends Commander implements DataHandledable { interface Redis extends EventEmitter { on(event: "message", cb: (channel: string, message: string) => void): this; + once(event: "message", cb: (channel: string, message: string) => void): this; on( event: "messageBuffer", - cb: (channel: string, message: Buffer) => void + cb: (channel: Buffer, message: Buffer) => void + ): this; + once( + event: "messageBuffer", + cb: (channel: Buffer, message: Buffer) => void ): this; on( event: "pmessage", cb: (pattern: string, channel: string, message: string) => void ): this; + once( + event: "pmessage", + cb: (pattern: string, channel: string, message: string) => void + ): this; on( event: "pmessageBuffer", - cb: (pattern: string, channel: string, message: Buffer) => void + cb: (pattern: string, channel: Buffer, message: Buffer) => void ): this; + once( + event: "pmessageBuffer", + cb: (pattern: string, channel: Buffer, message: Buffer) => void + ): this; + + on(event: "error", cb: (error: Error) => void): this; + once(event: "error", cb: (error: Error) => void): this; + + on(event: RedisStatus, cb: () => void): this; + once(event: RedisStatus, cb: () => void): this; // base method of EventEmitter - on(eventName: string | symbol, listener: (...args: any[]) => void): this; + on(event: string | symbol, listener: (...args: any[]) => void): this; + once(event: string | symbol, listener: (...args: any[]) => void): this; } applyMixin(Redis, EventEmitter); diff --git a/test/typing/events.test-.ts b/test/typing/events.test-.ts new file mode 100644 index 00000000..11e96ad6 --- /dev/null +++ b/test/typing/events.test-.ts @@ -0,0 +1,68 @@ +import { expectType } from "tsd"; +import { Redis } from "../../built"; + +const redis = new Redis(); + +expectType(redis.on("connect", () => {})); +expectType(redis.on("ready", () => {})); +expectType(redis.on("close", () => {})); +expectType(redis.on("end", () => {})); +expectType( + redis.on("error", (error) => { + expectType(error); + }) +); + +expectType(redis.once("connect", () => {})); +expectType(redis.once("ready", () => {})); +expectType(redis.once("close", () => {})); +expectType(redis.once("end", () => {})); +expectType( + redis.once("error", (error) => { + expectType(error); + }) +); + +redis.on("message", (channel, message) => { + expectType(channel); + expectType(message); +}); + +redis.on("messageBuffer", (channel, message) => { + expectType(channel); + expectType(message); +}); + +redis.on("pmessage", (pattern, channel, message) => { + expectType(pattern); + expectType(channel); + expectType(message); +}); + +redis.on("pmessageBuffer", (pattern, channel, message) => { + expectType(pattern); + expectType(channel); + expectType(message); +}); + +redis.once("message", (channel, message) => { + expectType(channel); + expectType(message); +}); + +redis.once("messageBuffer", (channel, message) => { + expectType(channel); + expectType(message); +}); + +redis.once("pmessage", (pattern, channel, message) => { + expectType(pattern); + expectType(channel); + expectType(message); +}); + +redis.once("pmessageBuffer", (pattern, channel, message) => { + expectType(pattern); + expectType(channel); + expectType(message); +});