Lesson 11 of 56 · Typescript
Mapped Types & `keyof`
Duration: 20 minutes
keyof Operator
type Person = { name: string; age: number; };
type PersonKeys = keyof Person; // "name" | "age"
Mapped Types – Transform each property
type ReadonlyPerson = { readonly [K in keyof Person]: Person[K] };
// equivalent to: { readonly name: string; readonly age: number; }
Partial & Required
type PartialPerson = Partial<Person>; // all props optional
type RequiredPerson = Required<PartialPerson>; // back to required
Record Utility
type RoleMap = Record<string, "admin" | "user" | "guest">;
const roles: RoleMap = { alice: "admin", bob: "user" };