ProgramGuru

Built-In Utility Types

Built-In Utility Types — the core idea

TypeScript utility types package common type transformations. Each utility has a distinct job: Readonly<T> describes non-writable properties, Partial<T> marks fields optional, and Record<K,V> describes an object with keys K and values V.

What you’ll learn

  • Explain built-in utility types with a concrete use case.
  • Run the example and account for every line of its output.
  • Recognize the difference between compile-time checking and runtime behavior.

Understand the rules and when to use them

Reading the code: Record requires both color keys and numeric values. Readonly restricts assignment through the view, but its JavaScript value is the same counts object.

Keep the boundary clear: Readonly is compile-time only and generally shallow. It does not freeze the object or make nested values deeply immutable.

See the idea as a flow

Built-In Utility Types — conceptual flowRecord<K, V> then Readonly<Record<...>> then same runtime objectBuilt-In Utility Types1Record<K, V>2Readonly<Record<...>>3same runtime objectType-level descriptions are erased; runtime actions need actual JavaScript.
Follow the three stages and identify what happens in TypeScript versus JavaScript.

Run the complete TypeScript example

Save the code as built-utility-types.ts. The result below was captured after compiling this exact example with strict TypeScript checking and executing the emitted JavaScript in Node.js.

TypeScript code

type Color = "red" | "blue";
const counts: Record<Color, number> = { red: 3, blue: 5 };
const view: Readonly<typeof counts> = counts;
console.log(view.red + view.blue);

Verified output

8

Build: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable built-utility-types.ts; run: node built-utility-types.js. The exact filename may be changed freely. When a multi-file project exists, prefer its tsconfig.json and build scripts.

Trace the example line by line

Record requires both color keys and numeric values. Readonly restricts assignment through the view, but its JavaScript value is the same counts object.

The output shown above comes from the initialized values in this program. Replacing a type annotation alone does not transform a JavaScript value.

Avoid this common mistake

Readonly is compile-time only and generally shallow. It does not freeze the object or make nested values deeply immutable.

Check both the TypeScript diagnostic and the emitted JavaScript behavior. A program can compile correctly and still need runtime validation or error handling.

Practice independently

Build a Partial<Record<Color,number>> and omit one color key to observe the resulting type.

Before running your change, write down the type you expect TypeScript to infer and the output you expect JavaScript to print. Then check both predictions.

Check your understanding

What is the runtime effect of Readonly<typeof counts>?
Choose one answer. You can try again.

Answer and explanation

None. Readonly is a static restriction; the object is not frozen.

Record requires both color keys and numeric values. Readonly restricts assignment through the view, but its JavaScript value is the same counts object.

What to remember

TypeScript utility types package common type transformations.

Try the practice change and re-run the exact program to test your understanding.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Partial, Required, and Readonly →