add typescript type mapping

This commit is contained in:
zekroTJA committed 2026-07-15 23:14:41 +02:00
1 parent 0dc3a6c0f6
commit 71fa2cc44c
1 file changed
+34
+34
View File
@@ -0,0 +1,34 @@
enum OpCode {
PING,
HELLO,
MOVE,
}
type HelloEvent = {
name: string;
};
type MoveEvent = {
x: number;
y: number;
};
type OpArgsMapping = {
[OpCode.PING]: null;
[OpCode.HELLO]: HelloEvent;
[OpCode.MOVE]: MoveEvent;
};
class WebSocketClient {
public op<TOp extends OpCode, TArgs extends OpArgsMapping[TOp]>(
op: TOp,
args: TArgs,
) {
// ...
}
}
function main() {
const client = new WebSocketClient();
client.op(OpCode.HELLO, { name: "foo" });
}