TypeScript · Generics and utility types
keyof
The union of an object type’s property names. Useful for typed property access.
Syntax
keyof TExample
Example
type User = { id: string; name: string };
function get<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
console.log(get({ name: "Ada" }, "name"));More in this group
Learn this in the course
Reference pages are the quick answer. The course explains the why, shows the full example and ends with a challenge.