Skip to content

Instantly share code, notes, and snippets.

@okovpashko
Created August 30, 2020 16:58
Show Gist options
  • Save okovpashko/da8cd3b872b9ad57465e99d984cbba30 to your computer and use it in GitHub Desktop.
Save okovpashko/da8cd3b872b9ad57465e99d984cbba30 to your computer and use it in GitHub Desktop.
// @flow
type Key = 'foo' | 'bar';
/**
* We must define an object type with an indexer that uses the same union type.
* Otherwise Flow will throw an error when we try accessing the value by a key
*/
const titles: {[Key]: string} = {
foo: 'This is a foo',
}
function getTitle(key: Key) {
if (key in titles) {
return titles[key];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment