Code Reference

Why TypeScript?

TypeScript · Reference cheat sheet

Why TypeScript?

TypeScript · Reference cheat sheet


📋 Overview

TypeScript exists to make JavaScript safer and clearer at scale. Static types document intent, unlock editor autocomplete, and catch whole classes of bugs before users see them.

🔧 Core concepts

BenefitWhat you gain
Early errorsTypos, wrong args, null misuse flagged in the editor
Better toolingJump-to-definition, rename, accurate autocomplete
Living docsSignatures explain APIs without outdated comments
RefactorsChange a type and follow red squiggles
Gradual adoptionRename .js.ts and tighten types over time

TypeScript is not a different runtime language — it compiles to JS that engines already understand.

💡 Examples

Bug caught at compile time:

function add(a: number, b: number): number {
  return a + b;
}

// add("2", "3"); // Error: string not assignable to number
console.log(add(2, 3));

Autocomplete-friendly objects:

type User = { id: number; name: string };

const user: User = { id: 1, name: "Sam" };
console.log(user.name.toUpperCase());

Optional fields and null safety (with strict settings):

type Profile = { bio?: string };

function bioLength(p: Profile): number {
  return p.bio?.length ?? 0;
}

console.log(bioLength({}));
console.log(bioLength({ bio: "hi" }));

Same idea in plain JS (fails later):

function add(a, b) {
  return a + b;
}
console.log(add("2", "3")); // "23" — silent logic bug

⚠️ Pitfalls

  • Types are not runtime validation — still validate untrusted input (APIs, forms).
  • Over-complex types can obscure simple code; keep beginner types plain.
  • as assertions and any defeat the purpose when overused.
  • Build setup cost is real; for a one-line snippet, plain JS may be enough.

On this page