Classes & patterns
19 steps in 5 sets of TypeScript.
Where the types meet real code. Classes with parameter properties and access modifiers, discriminated Result types for errors, builders and dependency injection, and typing things that involve time or caching.
Nineteen steps. The Result set is the interesting one: TypeScript makes errors-as-values pleasant enough that a lot of codebases have stopped throwing.
Classes
- Abstract classes and genericsA generic base class with an abstract contract.
- Class membersAccess modifiers, parameter properties, and the two kinds of private.
- Abstract classesA base that cannot be instantiated, with members a subclass must supply.
- MixinsA function taking a class and returning an extended one.
Results & validation
- A Result typeErrors in the return type, so the caller cannot forget them.
- Result union typeA discriminated union for success or failure, so errors are values the compiler checks.
- Validating unknown dataParsing at the boundary, so the inside of the program is typed.
- Result-typed JSON parseParses JSON into a Result rather than throwing, so the caller has to look.
Building & injecting
- A typed builderThe generic tracks which fields have been supplied so far.
- Dependency injection by typeDepend on an interface; the caller supplies whatever satisfies it.
- Tiny typed storeA tiny observable store, typed on its state shape.
- Generic stackA generic stack, and a first look at what a type parameter buys you.
- Generic queueA generic FIFO queue, the counterpart to the stack.
Timing & memory
- Typed memoizeMemoization that keeps the original signature intact.
- Typed debounceDebounce with the wrapped function's argument types preserved.
- Typed throttleThrottle with the wrapped function's signature preserved.
- Typed sleepA typed delay, the building block for everything else async here.
- Async retryRetries an async operation with backoff, typed on what it resolves to.
Encore
- schema.tsA tiny schema validator where the parsed type is inferred from the schema.