LESSON 03 / 6·2 min readIntermediate

What are union, intersection, keyof and typeof?

Try code in the playground
01 Understand02 Recall03 Prepare

Understand the concept

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.

Important ideas

  • 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.

PAUSE & RECALL

Can you explain it without the notes?

What are union, intersection, keyof and typeof?

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

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.

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

  • Runtime typeof returns a string; type-position typeof is a compile-time type query. They solve different problems.
Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: What are unknown, any and never?

Adapted from 01_JAVASCRIPT_TYPESCRIPT.md · “What are union, intersection, keyof and typeof?”. Examples are study fragments; framework behavior can depend on your version.