0 day streak
Code library

Expert TypeScript exercises

Read these Expert TypeScript snippets line by line — each one comes with a written breakdown of what the code does and why.

TypeScript · Expert

Recursive Template Literal Type Parsing

This snippet uses template literal types and recursive conditional types to extract dynamic path parameters from a string literal at compile time. It demonstrates how TypeScript can parse strings t…

SyntaxData Types
Open snippet →
TypeScript · Expert

Nominal Typing via Branded Types

TypeScript uses structural typing, but sometimes we need nominal typing to prevent accidental mixing of logically different primitives (like different currencies or IDs). Branding 'tags' a type wit…

SecurityBest Practices
Open snippet →
TypeScript · Expert

Explicit Resource Management with 'using'

Introduced in TypeScript 5.2, the 'using' declaration leverages the 'Symbol.dispose' hook to ensure resources like file handles or database connections are cleaned up automatically when the block s…

SyntaxBest Practices
Open snippet →
TypeScript · Expert

Contextual Metadata with Stage 3 Decorators

Stage 3 decorators provide a type-safe way to wrap class members. Unlike older experimental decorators, these use a structured 'context' object containing the member name, private/static status, an…

OOPDesign Patterns
Open snippet →
TypeScript · Expert

Assertion Signatures for Runtime Guarding

Assertion signatures use the 'asserts' keyword to tell the compiler that if a function returns normally, a certain condition must be true. This narrows types across the remainder of the containing…

Control FlowFunctions
Open snippet →
TypeScript · Expert

Exhaustiveness Checking with the 'never' Type

By assigning a variable to the 'never' type in a default branch, you create a compile-time check for exhaustiveness. If a new member is added to the 'Shape' union, the code will fail to compile unt…

Control FlowError Handling
Open snippet →
TypeScript · Expert

Variance Annotations with in/out Modifiers

TypeScript 4.7 introduced explicit variance annotations 'in' and 'out' for generic type parameters. 'out T' marks a covariant position (the type only appears in outputs), 'in T' marks contravarianc…

SyntaxData Types
Open snippet →
TypeScript · Expert

Async Pagination with Symbol.asyncIterator

Implementing Symbol.asyncIterator turns a cursor-driven data source into a first-class for-await-of iterable. The async generator stores the cursor on the call stack rather than in instance fields,…

Async & ConcurrencyDesign PatternsOOP
Open snippet →
TypeScript · Expert

Deterministic Resource Cleanup with `await using`

`await using` (TC39 Explicit Resource Management, supported since TypeScript 5.2) binds an async-disposable to a block scope and invokes its `Symbol.asyncDispose` method when the scope exits — even…

Async & ConcurrencyDesign PatternsError Handling
Open snippet →
TypeScript · Expert

Branded Email Types via Smart Constructors

TypeScript's structural type system normally treats every `string` as interchangeable, so validated values silently mix with raw input. A branded type tags a primitive with a phantom `unique symbol…

Data TypesSecurityDesign Patterns
Open snippet →
TypeScript · Expert

Recursive Tuple Reversal at the Type Level

Recursive conditional types combined with variadic tuple spreads let you compute on arrays at the type level — not just at the value level. `Reverse` splits the head off, recurses on the tail, and…

Arrays & ListsData Types
Open snippet →
TypeScript · Expert

Deep Nested Property Path Type Extraction

This snippet demonstrates compile-time template literal type recursions to derive dot-notated string paths for deeply nested object structures. It provides complete type safety when reading deeply…

Data TypesSyntax
Open snippet →