typestar

Interfaces and type aliases in TypeScript

Describing object shapes, and combining them.

interface User {
  readonly id: number;
  name: string;
  email?: string;
}

interface Admin extends User {
  permissions: string[];
}

type Point = { x: number; y: number };
type Labeled = Point & { label: string };

const admin: Admin = {
  id: 1,
  name: "ada",
  permissions: ["read", "write"],
};

How it works

  1. readonly and ? mark immutable and optional fields.
  2. extends builds a wider interface.
  3. & intersects two types into one.

Keywords and builtins used here

The run, in numbers

Lines
18
Characters to type
287
Tokens
77
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 49 seconds.

Type this snippet

Step 1 of 5 in Objects & interfaces, step 11 of 23 in Types & annotations.

← Previous Next →