Created
October 31, 2022 09:42
-
-
Save nicksheffield/ab24835800f963a75e54f8c53296abb3 to your computer and use it in GitHub Desktop.
Typed keyof output
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
const obj = { | |
str: 'hello', | |
num: 1, | |
bool: true | |
} | |
type MiscObject = typeof obj | |
const fun = <T extends keyof MiscObject>(key: T): MiscObject[T] => { | |
return obj[key] | |
} | |
const val = fun('str') // type string | |
const val2 = fun('num') // type number | |
const val3 = fun('bool') // type boolean |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment