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.
Awakening
How TypeScript koans test values and types, and how TypeScript works types out.
Object types
Describing objects: interfaces and aliases, optional and readonly properties, index signatures, and types compared by shape.
- Interfaces
- Type aliases
- Optional and readonly
- Index signatures and records
- Structural typing
- Object methods
Arrays and tuples
Arrays, readonly arrays, tuples, and the types array methods give back.
Unions and narrowing
Values of several types, and how checks in the code narrow them down.
- Unions
- typeof narrowing
- Truthiness and equality
- in and instanceof
- Discriminated unions
- Type predicates
- Assertion functions
- Exhaustiveness
Functions
Typing parameters and results, function types, overloads, and this.
Generics
Functions and types that work for any type, keeping track of which one.
Type operators
Making types from other types: keyof, typeof, indexed access, conditional and mapped types, and template literals.
Utility types
The generic types TypeScript comes with for transforming others.
- Partial, Required and Readonly
- Pick and Omit
- Exclude, Extract and NonNullable
- Function utility types
- Record and InstanceType
Classes
Typed classes: parameter properties, access modifiers, abstract classes, and implementing interfaces.
- Classes and parameter properties
- Access modifiers
- Abstract classes
- implements
- Static and generic classes
Enums, satisfies and more
Enums and their alternatives, satisfies, branded types, and typing async code.
Advanced types
Types that refer to themselves, distribution and how to stop it, defaults, merging declarations, constructor types and decorators.
Type puzzles
Small programs written in types: tuples, strings and numbers, computed by the compiler.