SuperAPI AI Directory

typescript-interface-vs-type

Coding & development

View repository

Updated 10-15 21:42

About this skill

TypeScript: Interface vs Type

Goal

Use interface until type features are needed. Prefer readable errors and compiler performance.

Rules

  • Use interface for object type definitions.
  • Use interface extends for extending object shapes.
  • Use interface for class contracts.
  • Use type for unions, tuples, primitives, mapped types, conditional types, and function aliases.
  • Prefer interface extends over object intersections.
  • Avoid intersections when properties may conflict.

Examples

interface User {
  name: string;
}

interface Admin extends User {
  permissions: string[];
}
type Status = "pending" | "approved" | "rejected";
type Point = [number, number];
type Handler = (event: Event) => void;

Why Prefer Extends

  • Conflicting properties fail at the definition.
  • Error messages are clearer.
  • Named interfaces are cached by TypeScript.
  • Intersections can be recomputed and harder to debug.

Avoid

type Admin = User & {
  permissions: string[];
};

Use this only when intersection semantics are intentional.

References

Output

  • Recommended declaration form.
  • Reason: object shape, union, tuple, conditional, mapped type, or extension.
  • Any intersection risk.