tsify
tsify copied to clipboard
Discriminated unions?
I was trying to switch
("match style") over enum cases with fields, but this seems not to be possible. Example (src):
type AppEvent =
| { kind: "click"; x: number; y: number }
| { kind: "keypress"; key: string; code: number }
| { kind: "focus"; element: HTMLElement };
function handleEvent(event: AppEvent) {
switch (event.kind) {
case "click":
// We know it is a mouse click, so we can access `x` and `y` now
console.log(`Mouse clicked at (${event.x}, ${event.y})`);
break;
case "keypress":
// We know it is a key press, so we can access `key` and `code` now
console.log(`Key pressed: (key=${event.key}, code=${event.code})`);
break;
case "focus":
// We know it is a focus event, so we can access `element`
console.log(`Focused element: ${event.element.tagName}`);
break;
}
}
Potential feature request, or maybe it's already possible and I'm missing something?