Last active
November 16, 2018 17:06
-
-
Save ehrenmurdick/ee9bc26b48ec1c26e8f886d0e30c912d 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
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