TypeScript · Generics and utility types

keyof

The union of an object type’s property names. Useful for typed property access.

Syntax
keyof T

Example

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

as const Awaited<T> Generic constraint Generic function Indexed access Parameters<T> Partial<T> Pick<T, K> / Omit<T, K>

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.

Open the course