Interface vs Type alias
ℹ️ They are similar in many ways. The key differences are: type can be used for primitives and type can be used for union of other types/interfaces.
Mostly, type alias is used to represent a more complex type like the union of a few different types:
Interfaces are used to represent the shape of an object (for example, to create classes or to use in function parameters):
Type aliases and interfaces are similar in many ways, except syntax. Nevertheless, they have differences that you need to know.
Similarities
An object can be represented using both interface and type:
Interfaces can create the same structure as type aliases, except types union and primitives:
It's also possible to merge interfaces or types:
Differences
Primitive types
It's possible to describe primitive only with the type alias:
As mentioned above, type alias can be used to describe the union:
Type Alias union
ℹ️ Note: a type alias with union can't be used inside interface or class anymore, what is logic, as class or interface can only represent a specific structure, not one or another.
Mapped Types
With type alias it's possible to create mapped types:
Declaration Merging is possible only with interfaces
You can define the same interface multiple times, and their definitions will be merged into one:
Read More
Last updated