What is the difference between “interface vs type” statements?

Technology CommunityCategory: TypeScriptWhat is the difference between “interface vs type” statements?
VietMX Staff asked 1 year ago
Problem
interface X {
    a: number
    b: string
}

type X = {
    a: number
    b: string
};

Unlike an interface declaration, which always introduces a named object type, a type alias declaration can introduce a name for any kind of type, including primitive, union, and intersection types.

By using type instead of interface the following capabilities are lost:

  • An interface can be named in an extends or implements clause, but a type alias for an object type literal cannot.
  • An interface can have multiple merged declarations, but a type alias for an object type literal cannot.