Skip to content

Instantly share code, notes, and snippets.

@willmtemple
Last active January 23, 2021 20:22
Show Gist options
  • Save willmtemple/da982366218c73b868cfc5dcef66982a to your computer and use it in GitHub Desktop.
Save willmtemple/da982366218c73b868cfc5dcef66982a to your computer and use it in GitHub Desktop.
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