mirror of
https://github.com/socketio/socket.io.git
synced 2026-04-30 03:00:39 -04:00
Syntax:
```ts
interface ServerToClientEvents {
"my-event": (a: number, b: string, c: number[]) => void;
}
interface ClientToServerEvents {
hello: (message: string) => void;
}
const socket: Socket<ServerToClientEvents, ClientToServerEvents> = io();
socket.emit("hello", "world");
socket.on("my-event", (a, b, c) => {
// ...
});
```
The events are not typed by default (inferred as any), so this change
is backward compatible.
Related: https://github.com/socketio/socket.io/issues/3742
14 lines
314 B
TypeScript
14 lines
314 B
TypeScript
import type * as Emitter from "component-emitter";
|
|
import { StrictEventEmitter } from "./typed-events";
|
|
|
|
export function on(
|
|
obj: Emitter | StrictEventEmitter<any, any>,
|
|
ev: string,
|
|
fn: (err?: any) => any
|
|
): VoidFunction {
|
|
obj.on(ev, fn);
|
|
return function subDestroy(): void {
|
|
obj.off(ev, fn);
|
|
};
|
|
}
|