Skip to content

Instantly share code, notes, and snippets.

@karlhorky
Created August 2, 2018 08:59
Show Gist options
  • Save karlhorky/be0331af480f4cdaef68cf79768adf5c to your computer and use it in GitHub Desktop.
Save karlhorky/be0331af480f4cdaef68cf79768adf5c to your computer and use it in GitHub Desktop.
Flow Type: Literal Type Generic Type
// @flow
// Originally from https://github.com/facebook/flow/issues/2639#issuecomment-409803679
const foo = 'FOO';
function literal(type) {
return { [type]: type };
}
type LiteralType<T> = $Keys<$Call<typeof literal, T>>;
('FOO': LiteralType<typeof foo>); // No errors
('BAR': LiteralType<typeof foo>); // Error: property `BAR`. Property not found in object literal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment