Learn TypeScript

JavaScript with a static type system that catches bugs before they run

36 lessons · 7 hours · 4 levels · covers TypeScript 5.9

TypeScript is a typed superset of JavaScript: it adds type annotations, interfaces, generics and a powerful type checker, then compiles to plain JavaScript that runs anywhere. It is the default choice for large front-end and Node.js codebases because it catches mistakes early, powers editor autocomplete and refactoring, and documents APIs in the code itself.

Before you start

TypeScript builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics11 lessons · 2 hours in total
2. PracticalUse it productively at work, with the usual idioms23 lessons · 4 hours in total
3. AdvancedThe less common features, performance and trade-offs31 lessons · 6 hours in total
4. ExpertInternals and the edge cases experts know36 lessons · 7 hours in total

The TypeScript tree

TypeScriptWhy TypeScriptWhy TypeScripttsc and tsconfig.jsontsc and tsconfig.jsonPrimitive and basic typesPrimitive and basictypesType annotations and inferenceType annotations andinferenceArrays and tuplesArrays and tuplesTyping functionsTyping functionsObject types and interfacesObject types andinterfacesUnion and literal typesUnion and literaltypesnull, undefined and strictNullChecksnull, undefined andstrictNullChecksType narrowingType narrowingany, unknown and neverany, unknown andneverType aliases vs interfacesType aliases vsinterfacesOptional and readonly propertiesOptional andreadonly propertiesEnums and const objectsEnums and constobjectsType predicates and assertion functionsType predicates andassertion functionsDiscriminated unionsDiscriminated unionsClasses in TypeScriptClasses in TypeScriptGenericsGenericsGeneric constraints and defaultsGeneric constraintsand defaultsBuilt-in utility typesBuilt-in utility typesTyping async codeTyping async codeModules, type imports and .d.ts filesModules, typeimports and .d.ts filesStrict mode and key compiler checksStrict mode and keycompiler checksVariables: let and constVariables: let andconstArraysArraysFunctionsFunctionsObjectsObjectsOptional chaining and ??Optional chaining and??ClassesClassesasync and awaitasync and awaitModules: import and exportModules: import andexport
Essentials and Practical shown (23 of 36 lessons); the full list is below. Click a lesson to read it.

All 36 lessons, in order

Getting started

  1. Why TypeScript · Essentials · 6 min

    TypeScript is JavaScript plus a static type layer that is checked at build time and erased before the code runs.

  2. tsc and tsconfig.json · Essentials · 12 min

    Install the compiler, run it on a project, and configure it with tsconfig.json.

Everyday types

  1. Primitive and basic types · Essentials · 8 min

    The everyday types: string, number, boolean, bigint, symbol, null, undefined, and the object and array forms built from them.

  2. Type annotations and inference · Essentials · 10 min

    Where you write types and where TypeScript works them out for you, including literal widening with let vs const.

  3. Arrays and tuples · Essentials · 10 min

    Type lists with T[], fixed-length positional data with tuples, and prevent mutation with readonly arrays.

  4. Typing functions · Essentials · 12 min

    Annotate parameters and return types, use optional, default and rest parameters, and write function types for callbacks.

  5. Object types and interfaces · Essentials · 12 min

    Describe object shapes inline or with interfaces, including optional properties, methods and extending.

Unions and narrowing

  1. Union and literal types · Essentials · 10 min

    Combine types with | and restrict values to exact strings or numbers with literal types.

  2. null, undefined and strictNullChecks · Essentials · 10 min

    Model missing values explicitly with T | undefined, and handle them with checks, ?. and ??.

  3. Type narrowing · Essentials · 12 min

    How TypeScript refines a union to a specific member using typeof, equality, in, instanceof and control flow.

  4. any, unknown and never · Essentials · 10 min

    The top and bottom types: why any switches checking off, why unknown is the safe alternative, and where never appears.

  5. Enums and const objects · Practical · 12 min

    Compare TypeScript enums with union types and as const objects, and know why many teams avoid enums.

  6. Type predicates and assertion functions · Practical · 12 min

    Write your own narrowing functions with value is T and asserts value is T.

  7. Discriminated unions · Practical · 12 min

    Model states and variants as a union of objects sharing a literal tag, and handle them exhaustively.

Objects, classes and interfaces

  1. Type aliases vs interfaces · Practical · 10 min

    When to use type and when to use interface, and how extends compares with intersections (&).

  2. Optional and readonly properties · Practical · 10 min

    Use readonly to prevent reassignment, understand what optional really means, and know the limits of both.

  3. Classes in TypeScript · Practical · 14 min

    Type class fields, use access modifiers, parameter properties, implements and abstract classes.

  4. satisfies, as const and const type parameters · Advanced · 12 min

    Check a value against a type without losing its precise inferred type, and keep literal types with as const.

Generics and functions

  1. Generics · Practical · 14 min

    Write functions and types that work over many types while keeping the link between input and output types.

  2. Generic constraints and defaults · Practical · 12 min

    Restrict type parameters with extends, use keyof in constraints, and give type parameters defaults.

  3. Built-in utility types · Practical · 12 min

    Derive new types from existing ones with Partial, Required, Readonly, Pick, Omit, Record, ReturnType and friends.

  4. Typing async code · Practical · 10 min

    Type promises and async functions, handle errors from catch, and type fetched data honestly.

  5. Function overloads · Advanced · 12 min

    Give one function several call signatures, and know when unions, generics or conditional types are better.

  6. Variance and function type compatibility · Advanced · 14 min

    When is Box<Dog> assignable to Box<Animal>? Covariance, contravariance, method bivariance and in/out annotations.

Modules, projects and the compiler

  1. Modules, type imports and .d.ts files · Practical · 14 min

    How TypeScript resolves imports, how to import types, and where types for JavaScript packages come from.

  2. Strict mode and key compiler checks · Practical · 12 min

    What strict turns on, and the extra checks worth enabling: noUncheckedIndexedAccess, exactOptionalPropertyTypes and more.

  3. Declaration merging and module augmentation · Advanced · 14 min

    How interfaces, namespaces and functions merge, and how to add types to third-party modules and globals.

  4. Project references and build mode · Expert · 15 min

    Split a large codebase into composite projects, build them incrementally with tsc -b, and keep editors fast.

  5. Compiler API and type-checking performance · Expert · 18 min

    Use the compiler API to analyse code, and diagnose slow type-checking with extendedDiagnostics and traces.

  6. Writing robust .d.ts for libraries · Expert · 18 min

    Publish accurate types for a library: package.json exports, ESM/CJS declarations, API design rules and type tests.

Type-level programming

  1. keyof, typeof and indexed access types · Advanced · 12 min

    Turn values into types with typeof, get property-name unions with keyof, and look up member types with T[K].

  2. Mapped types · Advanced · 14 min

    Build object types by iterating over keys with { [K in Keys]: ... }, adding or removing modifiers and remapping keys.

  3. Conditional types and infer · Advanced · 15 min

    Choose types with T extends U ? X : Y, understand distribution over unions, and extract parts of types with infer.

  4. Template literal types · Advanced · 12 min

    Build and parse string types with backtick syntax, combine them with unions, mapped types and infer.

  5. Structural typing and assignability rules · Expert · 15 min

    The precise rules behind 'is A assignable to B': freshness, weak types, {} vs object vs unknown, comparability and branded types.

  6. Type-level programming patterns · Expert · 18 min

    Recursive types, tuple manipulation, accumulators and tail recursion, plus how to test types and when to stop.