In this short article, I will guide you through the decision process for how to decide whether to use type
or interface
when using TypeScript. For this purpose I have created a table, which clearly shows what the main differences between type
and interface
are. The table will help you better understand the conclusion on how to choose between the two:
can be used for any type | can be augmented(merged) | can be extended | |
---|---|---|---|
type | Yes | No | No* |
interface | No (only used for object/class) | Yes | Yes |
* type can’t technically be extended via the “extends” keyword, but the same effect can be used by using intersection
So based on the information below, we can draw the folloing conclusions:
Use interface
if you need to define an object
type and/or it needs to be extended(via the extends
keyword) or augmented(redeclard and enhanced elsewhere in the code).
Use type
if the need to define types other than object
(e.g. union type, array etc), which doesn’t need to be extended or augmented.