add typestate typescript example
This commit is contained in:
1 parent
e42d897d41
commit
1d9a7fa4fe
4 files changed
+33
-2
No files matched your search
@@ -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, {});
|
||||
}
|
||||
Reference in new issue
Block a user