Skip to content

Instantly share code, notes, and snippets.

@nicksheffield
Created October 31, 2022 09:42
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 nicksheffield/ab24835800f963a75e54f8c53296abb3 to your computer and use it in GitHub Desktop.
Save nicksheffield/ab24835800f963a75e54f8c53296abb3 to your computer and use it in GitHub Desktop.
Typed keyof output
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