Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidystephenson/6d27f7fcba6d7b5fb12ba3348d545905 to your computer and use it in GitHub Desktop.
Save davidystephenson/6d27f7fcba6d7b5fb12ba3348d545905 to your computer and use it in GitHub Desktop.
const o = { message: 'hi', count: 5 }
// type Output = {
// message: string
// count: number
// }
type Output = typeof o
const results = []
function getValue <T extends keyof Output> (data: Output, key: T) {
console.log(`I will get the ${key} from the data`)
const value = data[key]
results.push(value)
return value
}
const message = getValue<'message'>(o, 'message')
function capitalize (text: string) {
console.log(text.toUpperCase())
}
capitalize(message)
const count = getValue(o, 'count')
function fix (number: number) {
console.log(number.toFixed(2))
}
fix(count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment