📎 Webclip
TypeScript: Type vs Interface
Anatoly Nevzorov’s distinction: interface can be reopened and merged across declarations, which suits public APIs and object shapes that might grow. type is declared once, cannot be redeclared, and only combines through intersections (&). In exchange, type handles unions, tuples, and conditional types, forms interface can’t represent at all.
In his own account, on large codebases interfaces compile faster and give smoother autocomplete, while complex union types can slow the TypeScript server down.
Fichamento #
- Declaring the same
interfacetwice merges the members into one type (interface Cat { meow }plus a laterinterface Cat { purr }gives aCatwith both). Declaring the sametypetwice throws a compiler error instead. - Type aliases cover unions (
'loading' | 'success' | 'error'), tuples ([number, number]), and conditional types (T | null | undefined), none of which an interface can express. - Interface merging is automatic; getting the same combined shape from types takes a manual intersection, as in
type User = Id & Name. - On large codebases, interfaces compile faster and give smoother autocomplete and refactoring; heavy union types can slow the TypeScript server down.
- His rule of thumb: interface for public APIs, object shapes, and anything expected to grow; type for unions, tuples, function overloads, and anything that needs
&or|. - An interface can extend an object-like type (
interface Dog extends Animal), but not one built from unions or primitives. A type can mimic an interface through&, though the result is more manual to maintain.