Utility types

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" }; console.log(draft.title, draft.done);

Write undefined

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 Todo = { title: string; done: boolean }; type Preview = Pick<Todo, "title">; type Open = Omit<Todo, "done">; const preview: Preview = { title: "Read" }; const open: Open = { title: "Read" }; console.log(preview.title, Object.keys(open).join(","));

Read title

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 Todo = { title: string; done: boolean }; type TodoDraft = Partial<Todo>; type Solid = Required<TodoDraft>; type Locked = Readonly<Todo>; const solid: Solid = { title: "Ship", done: false }; const locked: Locked = { title: "Read", done: true }; console.log(solid.title, solid.done, locked.title);

Ship false Read

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does Partial<Todo> change?

    Answer

    It makes every property optional

    Partial adds the question mark to every key, so a draft can provide only a few fields.

  2. How do Pick and Omit differ?

    Answer

    Pick keeps the listed keys; Omit drops the listed keys

    Pick builds a type from the names it is given, and Omit builds one without them.

  3. What does Record<string, number> describe?

    Answer

    An object whose keys are strings and values are numbers

    Record maps the given key type to the given value type, which is a typed dictionary.

  4. What is the difference between Required and Readonly?

    Answer

    Required makes fields mandatory; Readonly forbids writes

    One removes optionality, the other removes the right to assign.

  5. What does this program print? type Todo = { title: string; done: boolean }; type TodoDraft = Partial<Todo>; const draft: TodoDraft = { title: "Write" }; console.log(draft.title, draft.done);

    Answer

    Write undefined

    The omitted done field is optional, so reading it gives undefined.

  6. What does this program print? type Todo = { title: string; done: boolean }; type Preview = Pick<Todo, "title">; type Open = Omit<Todo, "done">; const preview: Preview = { title: "Read" }; const open: Open = { title: "Read" }; console.log(preview.title, Object.keys(open).join(","));

    Answer

    Read title

    Pick keeps title, Omit drops done, so both objects hold only the title.

  7. What does this program print? type Scores = Record<string, number>; const scores: Scores = { ada: 10, lin: 7 }; console.log(scores.ada);

    Answer

    10

    The ada key was given the value 10.

  8. What does this program print? type Todo = { title: string; done: boolean }; type TodoDraft = Partial<Todo>; type Solid = Required<TodoDraft>; type Locked = Readonly<Todo>; const solid: Solid = { title: "Ship", done: false }; const locked: Locked = { title: "Read", done: true }; console.log(solid.title, solid.done, locked.title);

    Answer

    Ship false Read

    The solid object holds false for done, and the locked object can still be read.

  9. What does the compiler say about this program? type Todo = { title: string; done: boolean }; type TodoDraft = Partial<Todo>; type Solid = Required<TodoDraft>; const s: Solid = { title: "Ship" }; console.log(s);

    Answer

    error TS2741: Property 'done' is missing in type '{ title: string; }' but required in type 'Required<Partial<Todo>>'.

    Required undoes Partial, so every field is required again and done is missing.

  10. What does the compiler say about this program? type Todo = { title: string; done: boolean }; type Locked = Readonly<Todo>; const locked: Locked = { title: "Read", done: true }; locked.title = "Write"; console.log(locked.title);

    Answer

    error TS2540: Cannot assign to 'title' because it is a read-only property.

    Readonly marks every property as a read-only view, so the assignment is refused.

Article author: Arthur Isaev