Programming Languages

TypeScript

JavaScript with a static type system on top. The checker runs at build time and the types are erased before the code runs.

Level: intermediate

What is it?

TypeScript adds static types to JavaScript. The compiler checks types and then erases them, emitting plain JavaScript, so there is no runtime cost and no runtime type checking. It provides interfaces, generics, union and intersection types, and strong inference, and any valid JavaScript is valid TypeScript.

Why does it matter?

Types catch a whole class of mistakes before the code runs and make large codebases navigable: autocomplete, safe renames, and precise contracts between modules. Most large JavaScript projects now use it.

Mental model

A spell-checker and a set of blueprints for a language that otherwise lets you write anything. The blueprints are removed before the building opens; what remains is ordinary JavaScript that has already been checked against them.

How it works

  • Structural typing: compatibility is by shape, not by declared name.
  • Union types and narrowing model 'one of several' precisely: type Shape = Circle | Rect, then switch on a discriminant.
  • Generics write reusable, type-safe code: function first<T>(xs: T[]): T | undefined.
  • strict mode enables null checks and other rules; turn it on for new projects.
  • Types exist only at compile time. Data from the network or storage still needs run-time validation.
  • any opts out of checking; unknown is the safe alternative that forces you to narrow first.

Steps

  • Write .ts source
  • Type-check
  • Erase types
  • Emit JavaScript
  • Run it anywhere JavaScript runs

Code example: A discriminated union and an exhaustive switch

type Shape =
  | { kind: "circle"; r: number }
  | { kind: "rect"; w: number; h: number };

function area(s: Shape): number {
  switch (s.kind) {
    case "circle": return Math.PI * s.r ** 2;
    case "rect": return s.w * s.h;
  }
}

console.log(area({ kind: "rect", w: 3, h: 4 }));   // 12

Real-world usage

  • Large front-end and Node.js codebases
  • Typed API clients and shared contracts
  • Libraries that need a safe public interface

Common mistakes

  • Sprinkling any and casts until the checker is silent and useless.
  • Trusting types on parsed JSON without validating it.
  • Fighting inference by annotating everything instead of the boundaries.

Interview questions

What does TypeScript compile to, and are types available at run time?

It compiles to plain JavaScript and erases the types, so they are not available at run time. Any run-time check must be written as ordinary JavaScript.

any versus unknown?

any disables checking for a value. unknown accepts any value but forces you to narrow it with a check before use, so it stays type-safe.

What is a discriminated union?

A union of object types that share a literal-typed field such as kind. Checking that field lets the compiler narrow to one member, and lets it warn when a case is missed.

Prerequisites

Related concepts

Continue learning

Browse the full computer science library