Interface (TypeScript)

An interface describes the shape of an object — which properties it has and what types they are. It is a contract the compiler checks against, with no effect at runtime.

TypeScript

Interfaces are one of TypeScript's most common features for typing objects and the props of components. If an object is missing a required property or has one of the wrong type, you get a compile error.

Interfaces can be extended to build on other interfaces, and they can be reopened to add members (declaration merging). For simple object shapes they are interchangeable with a type alias; a common convention is to use interfaces for object shapes that may be extended.

Syntax

interface Name {
  property: type;
}

Examples

TypeScript

interface User {
  name: string;
  age: number;
}

const u: User = { name: "Ana", age: 30 };
console.log(u.name);

Output:

Ana

TypeScript — Compile error: Property 'age' is missing in type '{ name: string; }'.

interface User {
  name: string;
  age: number;
}

const bad: User = { name: "Ana" };

Learn it in a lesson

See also

Back to the Dictionary