What’re the Interfaces in TypeScript, and What’s the Difference Between Type Aliases and Interfaces?
I’ve been learning TypeScript for a while and, I decided to publish what I learned about TypeScript. Long story short, Let’s take a brief look at interfaces.
1. What’s the Interface?
Interface as a Type
Line of explanations :
- We define interfaces using the “interface” keyword and add a name beside the interface keyword.
2/3. We could add properties in the interfaces with their type.
4. If we have an optional property in an interface, we must add the “?” prefix in front of the property name.
5. We also could add methods in the interfaces with its return type.
Difference Between Interfaces and Type Aliases:
- Declaration merging doesn’t work with type aliases, while it works properly with interfaces. Let me explain it. You can define the same interface multiple times, so its definitions will be merged into one.
this usage doesn’t work with type aliases because the type is a unique type unit.