keyof and typeof
TypeScript · Reference cheat sheet
keyof and typeof
TypeScript · Reference cheat sheet
📋 Overview
typeof (in type position) captures the type of a value. keyof T produces a union of T’s public property names. Together they type-safe key access, config maps, and derived APIs without duplicating shapes.
🔧 Core concepts
typeof value— type query from a runtime value / import.keyof T—string | number | symbolkeys (often string literal unions).- Indexed access —
T[K]whereK extends keyof T. keyof typeof obj— keys of a concrete object / const.- Mapped types —
\{ [K in keyof T]: … \}transform properties.
💡 Examples
const user = {
id: 1,
name: "Ada",
active: true,
} as const;
type User = typeof user;
type UserKey = keyof typeof user; // "id" | "name" | "active"
function getProp<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
getProp(user, "name"); // "Ada"
// getProp(user, "email"); // error
type Person = { name: string; age: number };
type PersonKeys = keyof Person; // "name" | "age"
type NameType = Person["name"]; // string
// Enum-like object
const Roles = {
Admin: "admin",
User: "user",
} as const;
type Role = (typeof Roles)[keyof typeof Roles]; // "admin" | "user"
// typeof on functions / classes
function createId(): string {
return crypto.randomUUID();
}
type IdFactory = typeof createId; // () => stringtype ReadonlyProps<T> = { readonly [K in keyof T]: T[K] };
type OptionalProps<T> = { [K in keyof T]?: T[K] };⚠️ Pitfalls
typeofin value position is JS runtime; in type position it’s erased.keyofon arrays includes number-like keys and"length"/ methods depending on type.keyof anyisstring | number | symbol— avoidanybases.- String index signatures make
keyofincludestring, weakening literals. typeofimports needimport type/ value import depending on emit settings.