Created
January 29, 2020 08:31
-
-
Save syxolk/e2e17d84043fd539bd9deed956d75031 to your computer and use it in GitHub Desktop.
Typescript keyof and "valueof"
This file contains 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
type Employee = { | |
canWalkVeryFast: boolean; | |
numberOfKeystrokes: number; | |
isNiceToTheBoss: "yes" | "no" | "sometimes"; | |
}; | |
function foobar<T extends keyof Employee>(key: T, value: Employee[T]) { | |
console.log(key, value); | |
} | |
// compiles | |
foobar("canWalkVeryFast", true); | |
foobar("numberOfKeystrokes", 100); | |
foobar("isNiceToTheBoss", "no"); | |
// does not compile | |
foobar("catsCuddled", 7); | |
foobar("isNiceToTheBoss", true); | |
foobar("isNiceToTheBoss", "often"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment