Skip to content

Commit

Permalink
fix: add types for known events (#1694)
Browse files Browse the repository at this point in the history
Co-authored-by: Zihua Li <i@zihua.li>
  • Loading branch information
trim21 and luin committed Apr 15, 2023
1 parent 89ebcb6 commit 1a87b23
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
43 changes: 42 additions & 1 deletion lib/Redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,48 @@ class Redis extends Commander implements DataHandledable {
}
}

interface Redis extends EventEmitter {}
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: 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: 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(event: string | symbol, listener: (...args: any[]) => void): this;
once(event: string | symbol, listener: (...args: any[]) => void): this;
}

applyMixin(Redis, EventEmitter);

addTransactionSupport(Redis.prototype);
Expand Down
68 changes: 68 additions & 0 deletions test/typing/events.test-.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { expectType } from "tsd";
import { Redis } from "../../built";

const redis = new Redis();

expectType<Redis>(redis.on("connect", () => {}));
expectType<Redis>(redis.on("ready", () => {}));
expectType<Redis>(redis.on("close", () => {}));
expectType<Redis>(redis.on("end", () => {}));
expectType<Redis>(
redis.on("error", (error) => {
expectType<Error>(error);
})
);

expectType<Redis>(redis.once("connect", () => {}));
expectType<Redis>(redis.once("ready", () => {}));
expectType<Redis>(redis.once("close", () => {}));
expectType<Redis>(redis.once("end", () => {}));
expectType<Redis>(
redis.once("error", (error) => {
expectType<Error>(error);
})
);

redis.on("message", (channel, message) => {
expectType<string>(channel);
expectType<string>(message);
});

redis.on("messageBuffer", (channel, message) => {
expectType<Buffer>(channel);
expectType<Buffer>(message);
});

redis.on("pmessage", (pattern, channel, message) => {
expectType<string>(pattern);
expectType<string>(channel);
expectType<string>(message);
});

redis.on("pmessageBuffer", (pattern, channel, message) => {
expectType<string>(pattern);
expectType<Buffer>(channel);
expectType<Buffer>(message);
});

redis.once("message", (channel, message) => {
expectType<string>(channel);
expectType<string>(message);
});

redis.once("messageBuffer", (channel, message) => {
expectType<Buffer>(channel);
expectType<Buffer>(message);
});

redis.once("pmessage", (pattern, channel, message) => {
expectType<string>(pattern);
expectType<string>(channel);
expectType<string>(message);
});

redis.once("pmessageBuffer", (pattern, channel, message) => {
expectType<string>(pattern);
expectType<Buffer>(channel);
expectType<Buffer>(message);
});

0 comments on commit 1a87b23

Please sign in to comment.