What is the difference between type and interface in TypeScript?

Answer

While type and interface overlap significantly for object types, their differences matter in practice. Similarities: both can describe object shapes, be extended/combined, and are erased at runtime. Differences: (1) Declaration merging — interfaces with the same name are automatically merged; type aliases cannot be re-declared. (2) What they can describe — type aliases can describe any type (unions, intersections, primitives, tuples, functions); interfaces can only describe object/class shapes and function signatures. (3) Extension syntax — interfaces use extends; type aliases use & for intersection. (4) Error messages — interfaces often produce more readable error messages in the compiler output. (5) Performance — interfaces are generally faster to check in complex codebases because TypeScript caches them. Community consensus: use interface for object shapes in public APIs; use type for unions, intersections, mapped types, and when you need the flexibility of a type alias.