TypeScriptInterview preparation
Intermediate·Concept·2 min read

What are union, intersection, keyof and typeof?

Practice this question

Definition:

A union allows a value to match one of several types, while an intersection requires it to satisfy all included types. keyof extracts property names from a type, and TypeScript's type-position typeof derives a type from a value declaration. Together they help express safe property access and reusable contracts.

Key points:

  • A | B: either type. A & B: must satisfy both types.

  • keyof User gives the property names of the User type.

  • type User = typeof user gets a type from a value; runtime typeof user returns a string.

  • For a union, narrow the value before using members that are not present on every possibility.

  • An intersection of incompatible property requirements can produce an impossible type such as never.

Interview point:

  • Runtime typeof returns a string; type-position typeof is a compile-time type query. They solve different problems.
Adapted from 01_JAVASCRIPT_TYPESCRIPT.md · “What are union, intersection, keyof and typeof?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.