TypeScript - Interface vs type
There are two main tools for declaring the shape of an object: interfaces and type aliases. They are very similar and, for the most common cases, they behave the same way.
However, there are some differences between these two syntaxes. There used to be more differences, but since TypeScript version 4+, both support extending other interfaces and types.
Some edge cases
- the inability to extend an interface from a type created using the | operator
- instead of intersection operation, there is a special extends keyword
- different error messages between types and interfaces
- interfaces are open on modification while types are closed
Interface
I can interchangeably switch between interface and type syntax to describe the type of an object
I know the syntax for interfaces and how to extend existing ones
Declaration merging
I know that the main difference between interfaces and types is the declaration merging mechanism
I understand the drawbacks and advantages of this mechanism and why it can be both useful and harmful depending on the needs and architecture of the code
I can interchangeably switch between interface and type syntax to describe the type of an object
I know the syntax for interfaces and how to extend existing ones
I know that the main difference between interfaces and types is the declaration merging mechanism
I understand the drawbacks and advantages of this mechanism and why it can be both useful and harmful depending on the needs and architecture of the code