This commit is contained in:
zekro committed 2026-07-17 10:28:20 +02:00
1 parent c367d3f3ca
commit 2bc148342d
3 files changed
+39 -18

No files matched your search

+18 -18
View File
@@ -1,34 +1,34 @@
enum OpCode {
PING,
HELLO,
MOVE,
PING,
HELLO,
MOVE,
}
type HelloEvent = {
name: string;
name: string;
};
type MoveEvent = {
x: number;
y: number;
x: number;
y: number;
};
type OpArgsMapping = {
[OpCode.PING]: null;
[OpCode.HELLO]: HelloEvent;
[OpCode.MOVE]: MoveEvent;
[OpCode.PING]: {};
[OpCode.HELLO]: HelloEvent;
[OpCode.MOVE]: MoveEvent;
};
class WebSocketClient {
public op<TOp extends OpCode, TArgs extends OpArgsMapping[TOp]>(
op: TOp,
args: TArgs,
) {
// ...
}
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" });
function main_ws() {
const client = new WebSocketClient();
client.op(OpCode.PING, {});
}
+13
View File
@@ -0,0 +1,13 @@
type ParseUrlParams<Url> = Url extends `${infer Start}/${infer Rest}`
? ParseUrlParams<Start> & ParseUrlParams<Rest>
: Url extends `{${infer Param}}`
? { [P in Param]: string }
: {};
function navigate<P extends string>(path: P, params: ParseUrlParams<P>) {
// ...
}
function main() {
navigate("/foo/bar", { foo: "asd" });
}
+8
View File
@@ -0,0 +1,8 @@
# Type Level Programming
## Sources
- https://www.emoses.org/posts/resolver-resolved-pattern/
- https://farazdagi.com/posts/2024-04-07-typestate-pattern/
- https://youtu.be/_ccDqRTx-JU
- https://type-level-typescript.com/