const person = { name: "wan", age: 28 } type Person = typeof person type PersonKeys = keyof person // "name" | "age" type PersonKTypes = Person[PersonKeys] // string | number
We can use "keyof" (a unit type) to build a lookup:
function getProperty<T, K extends keyof T>(obj: T ,key: K): T[K] { return obj[key] } const personName = getProperty(person, 'name')