koans.app

TypeScript Koans

TypeScript is JavaScript with types. You describe the shape of your data, and the compiler checks your code before it runs, catching mistakes and powering your editor’s suggestions; then the types disappear and plain JavaScript runs. Many large JavaScript projects are written in it. These koans explore its unusually rich type system: unions and narrowing, generics, and types computed from other types.

65 koans in 12 groups, tested with Vitest. Each is a small program whose tests fail: replace every ___ until they pass.

Start the path

Awakening

How TypeScript koans test values and types, and how TypeScript works types out.

  1. Expectations
  2. Inference
  3. Literal types
  4. any and unknown
  5. void and never
  6. as const

Object types

Describing objects: interfaces and aliases, optional and readonly properties, index signatures, and types compared by shape.

  1. Interfaces
  2. Type aliases
  3. Optional and readonly
  4. Index signatures and records
  5. Structural typing
  6. Object methods

Arrays and tuples

Arrays, readonly arrays, tuples, and the types array methods give back.

  1. Arrays
  2. Readonly arrays
  3. Tuples
  4. Array method types

Unions and narrowing

Values of several types, and how checks in the code narrow them down.

  1. Unions
  2. typeof narrowing
  3. Truthiness and equality
  4. in and instanceof
  5. Discriminated unions
  6. Type predicates
  7. Assertion functions
  8. Exhaustiveness

Functions

Typing parameters and results, function types, overloads, and this.

  1. Parameters and results
  2. Optional, default and rest
  3. Function types
  4. Overloads
  5. this parameters

Generics

Functions and types that work for any type, keeping track of which one.

  1. Generic functions
  2. Constraints
  3. Generic types
  4. const type parameters

Type operators

Making types from other types: keyof, typeof, indexed access, conditional and mapped types, and template literals.

  1. keyof and typeof
  2. Indexed access
  3. Conditional types
  4. infer
  5. Mapped types
  6. Key remapping
  7. Template literal types

Utility types

The generic types TypeScript comes with for transforming others.

  1. Partial, Required and Readonly
  2. Pick and Omit
  3. Exclude, Extract and NonNullable
  4. Function utility types
  5. Record and InstanceType

Classes

Typed classes: parameter properties, access modifiers, abstract classes, and implementing interfaces.

  1. Classes and parameter properties
  2. Access modifiers
  3. Abstract classes
  4. implements
  5. Static and generic classes

Enums, satisfies and more

Enums and their alternatives, satisfies, branded types, and typing async code.

  1. Enums
  2. Const objects instead of enums
  3. satisfies
  4. Branded types
  5. Async types

Advanced types

Types that refer to themselves, distribution and how to stop it, defaults, merging declarations, constructor types and decorators.

  1. Recursive types
  2. Distribution
  3. Defaults and NoInfer
  4. Declaration merging
  5. Constructor types
  6. Decorators

Type puzzles

Small programs written in types: tuples, strings and numbers, computed by the compiler.

  1. Tuple tricks
  2. String types
  3. Type arithmetic
  4. Typed events