ProgramGuru

Pick, Omit, and Record

Pick, Omit, and Record — the core idea

Pick<T,K> keeps selected keys, Omit<T,K> removes chosen keys, and Record<K,V> creates a key/value object shape. These are particularly useful for public API views that intentionally exclude internal fields.

What you’ll learn

  • Explain pick, omit, and record 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: Pick selects id and email. Omit also has precisely those two fields here. Record allows string keys such as primary, each pointing to a public account shape.

Keep the boundary clear: Omit does not erase sensitive fields from an existing runtime object. Construct a new object to avoid exposing unwanted data.

See the idea as a flow

Pick, Omit, and Record — conceptual flowAccount all fields then Pick / Omit then public object createdPick, Omit, and Record1Account all fields2Pick / Omit3public object createdType-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 pick-omit-record.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 Account = { id: number; email: string; passwordHash: string };
type Public = Pick<Account, "id" | "email">;
type WithoutSecret = Omit<Account, "passwordHash">;
const user: Public = { id: 1, email: "team@example.test" };
const indexed: Record<string, WithoutSecret> = { primary: user };
console.log(indexed.primary.email);

Verified output

team@example.test

Build: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable pick-omit-record.ts; run: node pick-omit-record.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

Pick selects id and email. Omit also has precisely those two fields here. Record allows string keys such as primary, each pointing to a public account shape.

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

Omit does not erase sensitive fields from an existing runtime object. Construct a new object to avoid exposing unwanted data.

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

Add createdAt:string to Account and decide how Pick and Omit differ after the change.

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

Does Omit<Account, "passwordHash"> remove a real object property?
Choose one answer. You can try again.

Answer and explanation

No. Omit describes a new type but does not remove properties at runtime.

Pick selects id and email. Omit also has precisely those two fields here. Record allows string keys such as primary, each pointing to a public account shape.

What to remember

Pick<T,K> keeps selected keys, Omit<T,K> removes chosen keys, and Record<K,V> creates a key/value object shape.

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: Exclude, Extract, and NonNullable →