TypeScript Reference ยท Concepts and Comparisons
type versus interface in TypeScript
Compare type aliases and interfaces: what each can express, declaration merging, extension, error messages and a simple rule for choosing.
Both type and interface name an object shape, and for plain objects they are interchangeable. They differ in what else they can do. A type alias can name any type, including unions, tuples, primitives and conditional types. An interface names only object-like shapes, but it can be reopened and extended with the same name.
Pick one convention for the project and apply it; the differences below decide the edge cases.
interface UserA { name: string } type UserB = { name: string };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Differences
| Feature | interface |
type |
|---|---|---|
| Object shapes | Yes | Yes |
| Unions, tuples, primitives, mapped and conditional types | No | Yes |
| Extending | extends (can extend classes too) |
& intersections |
| Declaration merging | Yes | No |
implements by a class |
Yes | Yes, if the type is an object shape |
| Computed or mapped members | No | Yes |
| Name in error messages | Kept | Often expanded to the structure |
Usage notes
- Rule of thumb: use
interfacefor public object contracts that others might extend or augment, andtypefor everything else. - Conflicting members behave differently.
extendsreports an error when a property is redefined incompatibly; an intersection silently producesneverfor that property. - Interfaces compare faster in large codebases because the checker caches them by name; heavy intersections can be slower.
- Index signature compatibility: an alias of an object literal type is assignable to
Record<string, unknown>, an interface is not, unless you add an index signature. - Library augmentation needs an interface, because merging is how you add fields to global or module types; see Declaration merging.
- Recursive types work in both; aliases now allow recursion through object members and arrays without special tricks.