Last active
January 26, 2021 06:51
-
-
Save mako-taco/99f834b1733c1ae6548bac995288d759 to your computer and use it in GitHub Desktop.
Union Types For Logging Events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum EventType { | |
SEARCH, | |
PROFILE_UPDATE, | |
CART_UPDATE | |
} | |
type Event = | |
| {type: EventType.SEARCH; query: string} | |
| {type: EventType.PROFILE_UPDATE; fields: string[]} | |
| {type: EventName.CART_UPDATE; newSubtotal: number; oldSubtotal: number} | |
; | |
function logEvent(event: Event) { | |
switch(event.type) { | |
case EventType.SEARCH: | |
return handleSearchEvent(event); | |
case EventType.PROFILE_UPDATE: | |
return handleProfileUpdateEvent(event); | |
case EventType.CART_UPDATE: | |
return handleCartUpdateEvent(event); | |
default: | |
throw enforceExhaustive(event.type) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment