AnyCompiler

Modules and the Standard Library

TypeScript code is often split across multiple files, connected with import and export — the same syntax modern JavaScript uses. This compiler runs a single file at a time, so multi-file imports can't be demonstrated directly here, but everything else in this lesson runs exactly as shown.

Utility types — building new types from existing ones

TypeScript ships a handful of built-in utility types that transform a type you already have. Partial<T> makes every property of T optional — useful for something like an update function where you only supply the fields that are changing.

interface Product {
  id: number;
  name: string;
  price: number;
}

function updateProduct(product: Product, changes: Partial<Product>): Product {
  return { ...product, ...changes };
}

const original: Product = { id: 1, name: "Keyboard", price: 50 };
const updated = updateProduct(original, { price: 45 });
console.log(updated);
Output
{ id: 1, name: 'Keyboard', price: 45 }

Pick<T, Keys> builds a new type with only the listed properties from T; Record<Keys, Type> builds an object type where every key from Keys maps to the same value type. Both are ways of deriving a new, precise type from one you've already defined, instead of writing it out by hand.

interface Product {
  id: number;
  name: string;
  price: number;
}

type ProductSummary = Pick<Product, "id" | "name">;

const summary: ProductSummary = { id: 1, name: "Keyboard" };
console.log(summary);
Output
{ id: 1, name: 'Keyboard' }

That's the core of TypeScript's type system on top of JavaScript. From here, the best next step is practice — pick a small idea and build it, leaning on the types to catch mistakes as you go.

Practice exercise

Given an interface User with id: number, name: string, and email: string, create a type PublicUser using Pick<User, "id" | "name">. Create a publicUser with id 1 and name "Alice", then print it.

← Type NarrowingMore lessons coming soon