Type vs Interface
Always aim for the highest type safety. Never use the
anytype.
We should always default to
typeunless there’s a valid use case for choosinginterface.
There’s a really good article Type vs Interface: Which Should You Use? by Matt Pocock that goes in-depth on this topic.
In summary:
-
Interfaces can’t express unions, mapped types, or conditional types, and all of these are very useful in day-to-day frontend work.
-
Interfaces with the same name in the same scope merge their declarations, leading to unexpected bugs.
-
Type aliases have an implicit index signature of
Record<PropertyKey, unknown>, which occasionally comes up. -
Basically, the only pro of using interface over type is inheritance.
extendsmakes TypeScript’s type checker run slightly faster than using&.