Skip to content

Lessons · TypeScript

Learn TypeScript step by step: 9 lessons with runnable examples

Each lesson pairs a plain-English explanation with code you can run. Every example was executed through the same sandbox the playground uses before it was published, so what you read has been checked, not just typed.

  1. Lesson 1

    TypeScript: Checked, Then Run

    What actually happens when TypeScript runs your code - a compiler checks the whole program against the rules of every value's type, and only a program with no errors ever reaches console.log.

    typescript · compiler · console.log · type-checking

  2. Lesson 2

    Basic Types: string, number, and boolean

    How TypeScript's three core primitive types work, the difference between writing a type annotation and letting the compiler infer one, and what happens the moment a value stops matching its type.

    types · type-annotations · type-inference · basics

  3. Lesson 3

    Arrays and Tuples

    How to type a growable list of same-kind values with an array type, how to type a fixed-length, position-specific group of values with a tuple, and why the two are not interchangeable.

    arrays · tuples · types

  4. Lesson 4

    Interfaces and Object Types

    How to name the shape of an object with interface, what structural typing means and why it lets unrelated interfaces stand in for each other, and how optional and readonly properties change what the compiler allows.

    interfaces · object-types · structural-typing

  5. Lesson 5

    Functions and Types

    How TypeScript requires a type on every function parameter, how return types are checked against what a function actually returns, and how optional and default parameters change a parameter's type.

    functions · parameters · return-types

  6. Lesson 6

    Union and Literal Types

    How to say a value can be one of several types with the | operator, how a literal type narrows a type down to one exact value, and how typeof and if let you use a union value safely.

    union-types · literal-types · narrowing

  7. Lesson 7

    Enums and Const Assertions

    How enum names a fixed set of related values you can refer to by a dotted name, how as const locks a value down to its most specific literal type, and where the two overlap with literal unions.

    enums · const-assertions · literal-types

  8. Lesson 8

    Generics Basics

    Why writing a function against any throws away type safety, how a generic type parameter lets one function work for every type while TypeScript still checks it, and how to write a simple generic constraint.

    generics · reusability · type-safety

  9. Lesson 9

    Type Narrowing and Guards

    How TypeScript narrows a union type across an if, the in operator, and a discriminated union's shared property, and how to write your own type predicate function to teach the compiler a new check.

    narrowing · type-guards · control-flow

Reading is half of it

The TypeScript exercises are where you write the code these lessons describe, one idea at a time, with the output checked for you. The playground is for trying any example on this page with your own numbers.