TypeScript - Objects, Interfaces & Type Aliases

Objects

type User = {
  id: string;
  name: string;
  email?: string;        // optional
  readonly createdAt: Date;
}

const u: User = { id: "u1", name: "A", createdAt: new Date() };
// u.createdAt = new Date(); // Error: readonly

Interfaces vs Type Aliases

interface Point { x: number; y: number }
interface Point3D extends Point { z: number }

type Result = { ok: true; value: T } | { ok: false; error: string };

Index Signatures

interface StringMap { [key: string]: string }

Implementing interfaces

interface Repository { get(id: string): Promise }
class InMemoryRepo implements Repository { constructor(private store = new Map()){}
  async get(id: string) { return this.store.get(id) ?? null }
}