add typestate typescript example

This commit is contained in:
zekro committed 2026-07-20 09:54:35 +02:00
1 parent e42d897d41
commit 1d9a7fa4fe
4 files changed
+33 -2

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
enum OpCode {
PING,
HELLO,
MOVE,
}
type PingEvent = Record<string, never>;
type HelloEvent = {
name: string;
};
type MoveEvent = {
x: number;
y: number;
};
type OpArgsMapping = {
[OpCode.PING]: PingEvent;
[OpCode.HELLO]: HelloEvent;
[OpCode.MOVE]: MoveEvent;
};
class WebSocketClient {
public send<TOp extends OpCode, TArgs extends OpArgsMapping[TOp]>(
op: TOp,
args: TArgs,
) {
// ...
}
}
function main1() {
const client = new WebSocketClient();
client.send(OpCode.PING, {});
}