keyof
keyof T is the union of T's public property names as string or number literal types. For { id: string; name: string } it is "id" | "name".
Detailed Explanation
When you write a function that takes an object and the name of one of its properties, the name should not be a free string. A free string lets the caller pass "nmae" and the compiler shrugs. keyof T is the set of names that actually exist.
function get<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
K extends keyof T restricts the second argument to real keys. T[K] is an indexed access type: the type of that property. Together they are the typed version of obj[key].
keyof follows the type you give it, not the value at runtime. keyof string is the names of string methods and number (because strings are indexable). keyof any is string | number | symbol. keyof never is string | number | symbol as well, which surprises people. keyof of a union is the intersection of keys: only names present on every member. keyof of an intersection is the union of keys. That pairing is the same covariant/contravariant flip you see elsewhere, and it is the reason keyof (A | B) is smaller than you expect.
Optional properties still appear in keyof. readonly does not remove a key. Numeric index signatures add number to the key type. String index signatures add string, which collapses the union — "id" | string is string — and you lose autocomplete of the known keys. That is one reason explicit listed properties plus a separate Record can be better than a wide index signature.
keyof results are often mapped. { [K in keyof T]: boolean } builds a flags object. { [K in keyof T]?: T[K] } is the idea behind Partial<T>. Template literal types can transform the keys: { [K in keyof T as get${Capitalize<K & string>}]: () => T[K] }.
There is no runtime keyof. Object.keys(obj) returns string[], not (keyof typeof obj)[], because objects can have extra properties the type does not mention. If you need a typed key list, build it as a const-asserted array and derive the type from the array, or write a generic that takes K extends keyof T.
Code Examples
1. A typed property lookup.
// 1. A typed property lookup.
type User = { id: string; email: string; age: number };
function pick<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
const user: User = { id: "u1", email: "ada@example.com", age: 36 };
const email = pick(user, "email"); // string
// pick(user, "role");
// Error: '"role"' is not assignable to '"id" | "email" | "age"'.2. keyof a union is the common keys only.
// 2. keyof a union is the common keys only.
type Cat = { name: string; purrs: boolean };
type Dog = { name: string; barks: boolean };
type Pet = Cat | Dog;
type PetKey = keyof Pet; // "name"3. Deriving a key union from a const array, not from Object.keys.
// 3. Deriving a key union from a const array, not from Object.keys.
const columns = ["id", "email"] as const;
type Column = (typeof columns)[number]; // "id" | "email"
function readColumn(user: User, column: Column): string {
return String(user[column]);
}
const keys = Object.keys(user);
// keys is string[], not (keyof User)[]Common Gotchas
`Object.keys` is `string[]` on purpose
casting it to `(keyof T)[]` lets you read keys the type never declared if the object has extras. Iterate a known key list instead.
A string index signature makes `keyof` collapse to `string`
listed keys stop being a useful union. Prefer `Record<SpecificKeys, V>` when the key set is closed.
`keyof (A | B)` is `keyof A & keyof B`
only shared keys are safe to read without narrowing the union first.
`keyof` includes number and symbol keys when they exist
intersecting with `string` (`K & string`) is the usual filter before you put a key in a template literal type.