Created
August 30, 2020 16:58
-
-
Save okovpashko/da8cd3b872b9ad57465e99d984cbba30 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
// @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