TypeScript interface vocabulary

  • interface vs type — equivalent for object shapes; interface supports declaration merging; type supports unions/mapped types
  • extends — inherits all properties from the parent interface; supports multiple parents; chains transitively
  • readonly — property can be set at creation but not reassigned; compile-time only, not enforced at runtime
  • prop?: Type — optional: the property may be absent or undefined; required if no ?
  • Declaration merging — same-named interfaces are combined; used to augment third-party types (e.g., Express Request)

Question 0 of 5

When does the choice between interface and type matter in TypeScript?

// These are equivalent: interface User { id: number; name: string; } type User = { id: number; name: string; }; // interface-only: declaration merging interface Window { myPlugin: () => void; } interface Window { theme: string; } // Result: Window has both myPlugin and theme // type-only: unions and intersections type StringOrNumber = string | number; type AdminUser = User & { role: "admin" };