Interfaces over aliases
Interfaces and type aliases in TypeScript are very similar, and you can use them interchangeably. However, some differences tip the scales in favor of interfaces.
Why prefer interfaces
First of all, errors in interfaces produce better error messages, for example:
Another one is extending interfaces has better performance than intersecting aliases.
Finally, while type aliases give more flexibility, it’s a bad thing when it comes to designing a database schema. Interfaces will make your code easier to read and maintain.
They will even force you to use best practices, as with the field groups:
For example, this would be the most intuitive way to define a post with a soft delete when using type aliases:
However, interfaces don’t support intersections, so you’ll have to use field groups that are ultimately better readability and type-safety-wise:
→ Read more about field groups
When to use type aliases
While interfaces are preferable, type aliases are still viable when defining a database schema, for example:
In the example above, we use a type alias to define a union type PostState
, which helps us define variable post state.