TypeScript Interface vs Type Alias
The Official Handbook provides some information about the difference between them.
Why I prefer to use type alias
over interface
because:
- Declaration merging: if the same interface (same name) is declared twice with different properties, they will be merged into one interface with all the properties. With type alias, it will trigger a TypeScript error.
- Usually we talk about types when we refer to TypeScript. It's commonly used the suffix
Type
when defining them. Files are often called*.types.ts
and folder./types/my.types.ts
. - It's not called InterfaceScript, isn't it? :)
Definitely. They are almost the same, there is no real benefit in most cases of using one over the other. What makes sense is to choose one and stick to it.