Type vs Interface
type
Interfaces can't express unions, mapped types, or conditional types.
- Type
aliasescan express any type.
interface
interface is always in object shape, not primitives.
interfacescan useextendsto inherit from anotherinterface.
It's a good rule of thumb to think of using interface when you consciously need extends.
When to use what
You should use types by default unless you need a specific feature of interfaces.
- you can technically blend and use
typeandinterfacetogether,
interface over type when type extension is involved
When you're working with objects that inherit from each other, use interfaces.
extendsmakes TypeScript's type checker run slightly faster than using&.
// extending interface interface IShrimpFriedRice extends IFriedRice { ingredients: IShrimp, } // extending type type Latte = { liquid: Coffee } & Milk