03 / 05

Explain the difference between 'interface' and 'type' in TypeScript.

Interfaces are good for specifying the shape of an object, eg for a person object, Type is for those data items that can not be represented as objects as well as objects.

Interface
  1. 1

    It's specifically intended for defining object shapes, including classes. It can also be used for extending other interfaces and defining contracts for objects.

  2. 2

    Interfaces can be extended to create new interfaces, allowing for easy reuse and modification

  3. 3

    It works better with classes since a class can implement an interface.

  4. 4

    Interfaces support declaration merging, meaning you can define multiple parts of an interface across your code, and TypeScript will combine them into one.

  5. 5

    Use interface when you are primarily dealing with object shapes and need features like declaration merging or integration with classes.

Type
  1. 1

    It's more versatile and can alias not just object shapes, but other types like primitives, unions, intersections, and more.

  2. 2

    Types can also be extended using intersections, but it's not as explicit as interface inheritance.

  3. 3

    Types can alias more complex types like union types, tuples, or primitives, which interfaces cannot do.

  4. 4

    Types do not support declaration merging. Once a type is defined, it cannot be redefined

  5. 5

    Use type when you need to define more complex types like unions, intersections, or primitives.

Difficulty: 5/10
Topics: interface vs type, declaration merging, extensibility

Scenario Questions

0-2 years experience
  1. 1

    How would you define a User shape using an interface versus a type alias? Show the syntax for each.

  2. 2

    If you need to add a new property to an existing User definition, can you do that with a type alias the same way you can with an interface? What would the code look like?

  3. 3

    What happens if you declare two type aliases with the same name in the same file? Does TypeScript merge them like it does with interfaces?

2-5 years experience
  1. 1

    We have a function that accepts either a string or an object with a length property. You used a type alias with a union, but adding a new property later caused a compiler error. How would you refactor using an interface, and why does it fix the issue?

  2. 2

    During a code review a teammate changed an exported type alias to an interface and the build started failing in another module that used intersection types. Explain why that break happened and how you'd resolve it.

  3. 3

    You notice declaration merging isn’t working for a type alias you created. What steps would you take to debug the problem?

5-8 years experience
  1. 1

    Our codebase uses type aliases for API response shapes, but we now need third‑party modules to augment those shapes with extra fields. Discuss the trade‑offs of switching to interfaces versus keeping type aliases, considering declaration merging, readability, and compile‑time performance.

  2. 2

    We are building a library that exposes a runtime class and a compile‑time options object. Should the options be modeled as an interface or a type alias, given we need conditional and mapped types? Justify your choice.

  3. 3

    A large monorepo has circular dependencies caused by type‑only imports of interfaces that also export values. Explain how using type aliases could help or hurt the situation, and propose a strategy to break the cycle.

8+ years experience
  1. 1

    Our organization is migrating a legacy JavaScript codebase to TypeScript and will introduce a shared type definition package. How would you decide whether to expose the shared contracts as interfaces or type aliases, considering future extensibility, versioning, and cross‑team consumption?

  2. 2

    We need to enforce a strict API contract across multiple services, some written in other languages. Discuss how the choice between interface and type alias impacts tooling such as code generation and OpenAPI, and the long‑term maintenance implications.

  3. 3

    In a polyglot microservices architecture we want to generate client SDKs from TypeScript definitions. What are the implications of using interfaces versus type aliases for the generation process, and how would you guide teams toward a consistent approach?

Follow-up Questions

  • When would you choose a type alias over an interface?
  • What limitations do interfaces have compared to type aliases?
  • How does declaration merging work with interfaces but not with type aliases?