Last active
January 23, 2021 20:22
-
-
Save willmtemple/da982366218c73b868cfc5dcef66982a to your computer and use it in GitHub Desktop.
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
interface DemoEnumPatternMap<T> { | |
Foo: (foo: Foo) => T, | |
Bar: (bar: Bar) => T | |
} | |
function matchDemoEnum<T>(value: DemoEnum, pattern: DemoEnumPatternMap<T>) : T { | |
switch (value.kind) { | |
case "Foo": | |
return pattern.Foo(value); | |
case "Bar": | |
return pattern.Bar(value); | |
} | |
} | |
for (const value of values) { | |
matchDemoEnum(value, { | |
Foo: ({ n }) => console.log(`Encountered a Foo with n: ${n}`), | |
Bar: ({ s }) => console.log(`Bar came with a message: ${s}`) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment