keyof, typeof & Indexed Access in TypeScript
From the TypeScript cheat sheet · Type Operations · verified Jul 2026
keyof, typeof & Indexed Access
Query types from existing values and access type properties by key
typescript
// keyof — union of all keys
type User = { name: string; age: number };
type UserKey = keyof User; // "name" | "age"
// typeof — get type from a value
const config = { port: 3000, host: "localhost" };
type Config = typeof config; // { port: number; host: string }
// Indexed access — look up a type by key
type Age = User["age"]; // number💡 keyof + generics is the foundation for type-safe property access functions
⚡ typeof gets a type from a VALUE — essential for config objects and function return types
📌 T[K] indexed access works on nested types too: User["address"]["city"]
🟢 Array[number] extracts the element type from an array — combine with as const for unions
keyoftypeofindexed-access