Examples
TypeScript examples
JavaScript with static types for dependable code.
TypeScript Types
Describe values before runtime.
TypeScript Union Types and Narrowing
Accept more than one type and narrow it safely with typeof.
TypeScript literal types and as const
Build a union of allowed strings from an object or array and make switch statements exhaustive.
TypeScript unknown and user-defined type guards
Validate parsed JSON at run time and let the compiler trust the result only after the check.
TypeScript tuples, readonly, and optional chaining
Return fixed-shape pairs, protect arrays from change, and read nested optional values safely.
TypeScript Interfaces
Describe the shape of an object, including optional properties.
TypeScript abstract classes and access modifiers
Use private, protected, readonly, and abstract to describe what subclasses and outside code may do.
TypeScript Generics
Write one function that keeps the type of whatever it receives.
TypeScript generic stack and queue
Write containers that work for any element type and keep it checked.
TypeScript generic binary search tree
Order any type with a comparator function and collect the values in sorted order.
TypeScript typed comparators and multi-key sorting
Build comparators from a key with keyof, and combine them to sort by several fields.
TypeScript: a typed memoize wrapper
Cache the results of a pure function with a generic higher-order function and count how often the body runs.
TypeScript: type-safe select, where, and orderBy
Write small query helpers whose column names are checked by the compiler with keyof.
TypeScript: a process lifecycle as a typed state machine
Allow only legal transitions between process states and reject the rest.
TypeScript: parsing a raw HTTP request into a typed object
Split the request line, headers, and body of an HTTP/1.1 message and read a query parameter.
TypeScript: explicit errors with a Result type
Return success or failure as a value, and let the compiler force the caller to handle both.
TypeScript: branded types for validated and escaped data
Use the type system so that unvalidated text cannot be passed where a validated value is required.
TypeScript: running async tasks with a concurrency limit
Start at most N tasks at once, collect results in input order, and measure the peak concurrency.
TypeScript: a generic breadth-first search solves the water-jug puzzle
Write the search once for any state type and apply it to two jugs.
TypeScript: propositional formulas as a discriminated union
Represent formulas as trees, evaluate them for any assignment, and test for tautologies.
TypeScript: precision, recall, and F1 from a confusion matrix
Count true and false positives and negatives with a typed interface, then compute the metrics.
TypeScript: composing neural-network layers behind an interface
Build a small network from Dense and Sigmoid layers that share one Layer interface.
TypeScript: a grayscale image class with threshold and flip
Wrap a flat pixel buffer in a class that checks bounds and returns new images from its operations.