Utility types in TypeScript
The built-in type transformers you reach for daily.
interface User {
id: number;
name: string;
email: string;
admin: boolean;
}
type Draft = Partial<User>;
type Frozen = Readonly<User>;
type Summary = Pick<User, "id" | "name">;
type Public = Omit<User, "email">;
type Lookup = Record<string, User>;
type Required2 = Required<Draft>;
const patch: Draft = { name: "ada" };
How it works
PartialandRequiredflip optionality.PickandOmitselect or remove fields.Recordbuilds a keyed map type.
Keywords and builtins used here
Draftbooleanconstinterfacenumberstringtype
The run, in numbers
- Lines
- 15
- Characters to type
- 320
- Tokens
- 87
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 52 seconds.
Step 3 of 4 in Reading types, step 7 of 20 in Generics.