Utility types
| Partial | |
| Pick and Omit | |
| Record maps | |
| Required and Readonly |
Partial
Partial makes every property optional in one word. Drafts, forms, and patch payloads all start here.
TodoDraft needs only the fields the caller provides. The draft const sets just the title and stays valid.
type Todo = {
title: string;
done: boolean;
};
type TodoDraft = Partial<Todo>;
const draft: TodoDraft = { title: "Write" };
Pick and Omit
Pick keeps the listed keys while Omit drops them. Both return fresh object types built from the original.
Preview carries only the title, and Open carries everything except done. Reach for these for list views and public shapes.
type Preview = Pick<Todo, "title">;
type Open = Omit<Todo, "done">;
const preview: Preview = { title: "Read" };
Record maps
Record builds an object type from a key set and a value type. It is the typed version of a plain dictionary.
Scores maps any string to a number, so both entries typecheck at once. Use a literal union for keys to lock the map down.
type Scores = Record<string, number>;
const scores: Scores = { ada: 10, lin: 7 };
console.log(scores.ada); // 10
Required and Readonly
Required is the mirror of Partial and makes every field mandatory again. Readonly freezes the type so no field can be reassigned.
Solid demands the full todo, while Locked can only be read. Combine them freely since each returns a plain object type.
type Solid = Required<TodoDraft>;
type Locked = Readonly<Todo>;
const solid: Solid = { title: "Ship", done: false };
Next: satisfies
Article author: Arthur Isaev