0 day streak
Code library

Intermediate TypeScript exercises

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

TypeScript · Intermediate

Mapped Types

Mapped types allow you to create new types based on the properties of an existing type. They are essential for transforming object structures dynamically.

SyntaxBest Practices
Open snippet →
TypeScript · Intermediate

Conditional Types

Conditional types select one of two possible types based on a condition expressed as a type relationship test.

SyntaxData Types
Open snippet →
TypeScript · Intermediate

Intersection Types

Intersection types combine multiple types into one, allowing you to merge existing types to create a type that has all the features you need.

SyntaxData Types
Open snippet →
TypeScript · Intermediate

Discriminated Unions

Discriminated unions use a common literal property (the discriminant) to allow TypeScript to narrow down members of a union safely.

Design PatternsBest PracticesControl Flow
Open snippet →
TypeScript · Intermediate

Generics with keyof Constraints

Using 'keyof' with generics ensures that a function parameter is a valid key of a specific object, providing full type safety and IDE autocompletion.

FunctionsBest PracticesSyntax
Open snippet →
TypeScript · Intermediate

Index Signatures for Dynamic Keys

Index signatures allow objects to have flexible keys that aren't known ahead of time. You define the type of the key (usually string or number) and the type of the value it returns.

SyntaxData Types
Open snippet →
TypeScript · Intermediate

Abstract Classes for Shared Logic

Abstract classes cannot be instantiated directly. They serve as templates, allowing you to define shared methods while forcing subclasses to implement specific 'abstract' logic.

OOPDesign Patterns
Open snippet →
TypeScript · Intermediate

User-Defined Type Guards

A type guard is a function that returns a type predicate ('pet is Fish'). It allows TypeScript to narrow down the type of an object within a specific code block after a runtime check.

Control FlowFunctions
Open snippet →
TypeScript · Intermediate

Template Literal Types

Template literal types allow you to create new string types by combining existing union types. This is extremely useful for generating predictable string patterns like CSS classes or action names.

SyntaxData Types
Open snippet →
TypeScript · Intermediate

Extracting Types with ReturnType

The ReturnType utility type extracts the return type of a function. This ensures that if the function's implementation changes, the dependent types update automatically, maintaining a single source…

FunctionsBest Practices
Open snippet →
TypeScript · Intermediate

Type-Safe Input Sanitization via Branded Types

Branded types create nominal type distinctions over primitive types like strings. This forces developers to pass user input through an explicit sanitization function before it can be accepted by se…

SecurityData TypesBest Practices
Open snippet →
TypeScript · Intermediate

Streaming Data Streams with Typed Async Generators

Async generators typed with AsyncGenerator<T, ReturnType, NextType> allow processing asynchronous sequences lazily. Consuming batches sequentially with for await...of keeps memory consumption low d…

Async & ConcurrencyControl FlowPerformance
Open snippet →
TypeScript · Intermediate

Developing a Type-Safe Event Bus Architecture

An Event Bus decouples publishers from subscribers across modular application layers. Leveraging mapped types and indexed accesses (`Events[K]`), TypeScript enforces exact payload contracts for eve…

FrameworksDesign PatternsAsync & Concurrency
Open snippet →