Skip to content

Instantly share code, notes, and snippets.

@ehrenmurdick
Last active November 16, 2018 17:06
Show Gist options
  • Save ehrenmurdick/ee9bc26b48ec1c26e8f886d0e30c912d to your computer and use it in GitHub Desktop.
Save ehrenmurdick/ee9bc26b48ec1c26e8f886d0e30c912d to your computer and use it in GitHub Desktop.
export function prop<O: { +[string]: mixed }, P: $Keys<O>>(
p: P
): (o: O) => $ElementType<O, P> {
return function(o) {
return o[p];
};
}
const f = prop('hi');
const a = {
hi: 'hi'
};
const b = {
hi: 2
};
(f(a): string); // this is happy, as a.hi is in fact a string
(f(b): string); // flow catches this, because b.hi is a number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment