What is the difference between interface and type alias in TypeScript?
Answer
Both interface and type can describe object shapes, but they have key differences. Interfaces: can only describe object shapes; support declaration merging (two interfaces with the same name are merged); are extended with extends; are generally preferred for defining object/class contracts. Type aliases (type): can describe any type — primitives, unions, intersections, tuples, functions; do NOT support declaration merging; use & for intersection (composition); required for union and tuple types. Examples only possible with type: type StringOrNumber = string | number;, type Point = [number, number];, type Callback = () => void;. When to choose: use interface for object shapes in public APIs (because of declaration merging and cleaner error messages); use type for unions, intersections, mapped types, and when describing non-object types. In practice, teams often pick one and stick with it — both are valid.