You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was trying to switch ("match style") over enum cases with fields, but this seems not to be possible. Example (src):
typeAppEvent=|{kind: "click";x: number;y: number}|{kind: "keypress";key: string;code: number}|{kind: "focus";element: HTMLElement};functionhandleEvent(event: AppEvent){switch(event.kind){case"click":
// We know it is a mouse click, so we can access `x` and `y` nowconsole.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` nowconsole.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?
The text was updated successfully, but these errors were encountered:
Hi @ivanschuetz, it seems you're referring to a TypeScript limitation (which I believe is solved in TypeScript v4.9, btw).
For type-safe pattern matching in TypeScript, I strongly recommend the excellent ts-pattern library.
I was trying to
switch
("match style") over enum cases with fields, but this seems not to be possible. Example (src):Potential feature request, or maybe it's already possible and I'm missing something?
The text was updated successfully, but these errors were encountered: