Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add type for known event of EventEmitter #1694

Merged
merged 2 commits into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});