Skip to the page

How TypeScript thinks · core

Types, interfaces and structural typing

TypeScript checks the shape of a value, not its name; type and interface both describe shapes, but only interfaces merge and only type aliases can name unions and other non-object types.

The notes

What you must know

  • Structural typing: any value with the required properties fits, even if it was never declared with that type. Extra properties are only rejected for fresh object literals (excess property check).
  • interface can be extended with extends and re-opened (declaration merging), which is how libraries let you add fields (e.g. to Window).
  • type can name anything: unions, tuples, primitives, mapped and conditional types. Intersections (A & B) combine types.
  • Types exist only at compile time. They are erased: nothing in the emitted JavaScript checks them.
  • Rule of thumb: interface for public object shapes that others extend; type for unions and computed types. Be consistent in a codebase.

The red pen

Common traps: mistake → fix

  • Mistake: "interface and type are the same"

    Fix: only interfaces merge; only types can be unions or mapped/conditional types.

  • Mistake: "TypeScript checks types at runtime"

    Fix: types are erased; runtime data from an API must be validated separately.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is the difference between type and interface?
  2. What is structural typing? Give an example where it surprises people.
  3. What is declaration merging and when is it useful?
  4. Why does an extra property error on an object literal but not on a variable?

Practise

Practise with these questions

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview