Skip to content

Instantly share code, notes, and snippets.

@syxolk
Created January 29, 2020 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syxolk/e2e17d84043fd539bd9deed956d75031 to your computer and use it in GitHub Desktop.
Save syxolk/e2e17d84043fd539bd9deed956d75031 to your computer and use it in GitHub Desktop.
Typescript keyof and "valueof"
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