Skip to the solution
EasyFrontend9 min read

What is the difference between interface and type?

859 views

The problem

Question presented to candidate: "In TypeScript you can write interface User { ... } or type User = { ... }. What is actually different between the two, and how do you decide which one to use?"

What a strong answer should cover:

  • For a plain object shape the two are interchangeable: same checking, both erased at run time, a class can implements either
  • Only type can name a union, a primitive, a tuple, a mapped or a conditional type; interface only describes object (and function or constructor) shapes
  • Extending: interface X extends Y reports a conflicting member as an error (TS2430), while Y & { ... } silently produces never for that member
  • Declaration merging: two interface declarations with the same name merge (used for module augmentation and global types); two type aliases with the same name are a duplicate identifier error (TS2300)
  • The subtle one: an object type alias is assignable to Record<string, T> (implicit index signature), an interface is not
  • A sensible team rule (interfaces for object shapes you may extend, type for unions and computed types) and the performance note: the TypeScript wiki prefers extends over large intersections

Code / implementation expected: Optional β€” the same shape written both ways, plus a union that only type can express.

  • types

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

9 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

TypeScriptThe same shape both ways, unions, merging and conflicts
TypeScriptImplicit index signatures: why an interface does not fit Record

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 4 of 81 in TypeScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

What is the difference between any and unknown?

Continue