# Functype > A functional programming library for TypeScript, using immutable data structures and type classes. Scala-inspired: Option, Either, Try, List, IO and more, with typed errors, dependency injection and do-notation. Version 2.0.0. Install with `npm install functype`. Every type listed below is also available from the CLI (`npx functype `) and the functype MCP server. - [llms-full.txt](https://functype.org/llms-full.txt): The guides, type pages and full API reference in one file - [API reference](https://functype.org/reference.md): Every public type's methods, generated from the CLI data ## Core - [Option](https://functype.org/option.md): Safe nullable handling - Some or None - [Either](https://functype.org/either.md): Error handling with Left (error) or Right (success) - [Try](https://functype.org/try.md): Wrap operations that may throw - Success or Failure - [Obj](https://functype.org/obj.md): Immutable object wrapper with fluent operations - [Identity](https://functype.org/reference.md#identity): The identity monad: wraps a value with no extra effect. Useful as a neutral container in generic code. ## Collection - [List](https://functype.org/list.md): Immutable array with functional operations - [Set](https://functype.org/set.md): Immutable set of unique values - [Map](https://functype.org/reference.md#map): Immutable key-value store - [LazyList](https://functype.org/reference.md#lazylist): Lazy sequences for large/infinite data - [Tuple](https://functype.org/reference.md#tuple): Fixed-size typed array - [Stack](https://functype.org/reference.md#stack): Immutable LIFO stack ## Effect - [IO](https://functype.org/io.md): Lazy effect type with typed errors and dependency injection - [Exit](https://functype.org/reference.md#exit): Outcome of running an IO effect — Success | Failure | Die | Interrupted, returned by .runExit(). Die carries a defect: a value that is not an E. Either has no branch for one, so .run() rejects on it. - [Http](https://functype.org/http.md): HTTP fetch wrapper returning IO>. Pass `decode: Decoder` (Either-returning) for typed responses; wrap a throwing parser such as Zod's .parse in `Decoder.fromThrowing(parse)`. 2.0 removed the `validate` field. Request bodies auto-flatten functype ADTs to primitives; `flatten: false` preserves tagged emission for functype-to-functype services. Per-call `params` (HttpQueryParams) appends query strings with proper encoding. Http.client accepts `beforeRequest` and `afterResponse` (both effectful IO transformers). `afterResponse` runs on the success path only — error handling belongs in .catchTag. Composes via .tap/.map/.flatMap/.catchTag/.retry/.retryWhile/.retryWithBackoff/.timeout. - [HttpError](https://functype.org/http.md): Three-variant ADT for HTTP failures: NetworkError | HttpStatusError | DecodeError (also exported as ResponseDecodeError alias) - [Decoder](https://functype.org/reference.md#decoder): Either-returning decoder contract: `Decoder = (raw: unknown) => Either`. Bundled combinators for primitives + functype ADTs (Option/Either/List/Map, null-bias). `Decoder.tagged.*` round-trips the `{_tag, value}` shape for functype-to-functype services. - [DecoderError](https://functype.org/reference.md#decodererror): Recursive ADT for decoder failures: Leaf | Composite. Children mirror the input tree so multi-field failures preserve structural paths. Distinct from `HttpError.DecodeError` — this is the inner structural cause that the HTTP wrapper carries. ## Utility - [Lazy](https://functype.org/reference.md#lazy): Deferred computation with memoization. Note: `serialize()` and `toValue()` FORCE the thunk (visible side effect) since a closure cannot be JSON-serialized — there is no representable 'unevaluated post-serialize' state. Thunk failures are captured via SerializedError and rethrown on access after fromJSON. - [Cond](https://functype.org/match.md): Conditional expression builder - replace if-else chains - [Match](https://functype.org/match.md): Pattern matching - replace switch statements - [Do](https://functype.org/do-notation.md): Generator do-notation across Option, Either, List and Try (DoAsync for promises). Short-circuits on None/Left/Failure/empty List. - [Ref](https://functype.org/reference.md#ref): Mutable reference cell for controlled, local mutability. Invariant in A by design (a mutable cell can't be widened). - [Brand](https://functype.org/reference.md#brand): Nominal typing without runtime overhead - [ValidatedBrand](https://functype.org/reference.md#validatedbrand): Branded types with runtime validation - [Wire](https://functype.org/wire.md): Serialization-boundary marker for a row, collection or nullable field at DB / HTTP / JSONB boundaries. Zero runtime, assignable both ways with T. prefer-list and prefer-option skip everything inside it. A collection is Wire>. - [invariant](https://functype.org/reference.md#invariant): One-line bug check: invariant(cond, msg) throws a tagged InvariantViolation when cond is falsy and narrows cond after it. For programmer errors only; expected failures return Either. - [TracedOption](https://functype.org/reference.md#tracedoption): Opt-in Option wrapper that emits one TraceEvent per combinator to an injected Tracer, then delegates. For code-path introspection. ## Validation - [TypedError](https://functype.org/reference.md#typederror): Code-tagged structured error ADT (extends Throwable); TypedError.validation(...) feeds accumulating validation. - [Validation](https://functype.org/reference.md#validation): Error-accumulating validation (functype's Validated role) — FormValidation collects all field errors, not fail-fast. ## Serialization - [Serialization](https://functype.org/reference.md#serialization): Universal `@functype`-marked JSON serialization. `deserialize` walks parsed JSON and reconstructs any functype value found — no type argument needed. Lenient: plain JSON passes through. Strict on unknown markers (defends against Effect/fp-ts `_tag` collision). 1.2.1 adds `toEnvelope`/`fromEnvelope` for nesting inside structured serializers (SuperJSON, DBOS), and `deserializeStrict` for boundaries that require functype on the wire. - [SerializedError](https://functype.org/reference.md#serializederror): Canonical Error projection used by Try.Failure and Lazy-with-thrown-thunk. Round-trips name + message + stack + cause chain. `e.name === 'TypeError'` survives; `instanceof TypeError` does NOT. ## Service - [Logger](https://functype.org/logger.md): Minimal 4-method logging interface every functype-* package targets. Type-only, no runtime. Impls live in consumers: consoleBootLogger (functype-os/config), DirectLogger (functype-log/direct). Tracer is type-only on the same terms; Clock/Random are not added. - [Tracer](https://functype.org/reference.md#tracer): One-method trace sink consumed by TracedOption. Type-only, like Logger; declare your own Tag for injection. - [SpanTracer](https://functype.org/reference.md#spantracer): Span contract and Tag for IO.withSpan: start, run (body with the span active), end. None provided: withSpan is a no-op. ## Guides - [AI Guide](https://github.com/jordanburke/functype/blob/main/packages/functype/docs/ai-guide.md): Guide for AI assistants writing functype code - [Quick Reference](https://github.com/jordanburke/functype/blob/main/packages/functype/docs/quick-reference.md): Concise guide to common patterns and operations - [Examples](https://github.com/jordanburke/functype/blob/main/packages/functype/docs/examples.md): Practical examples for the major types - [functype vs Effect](https://github.com/jordanburke/functype/blob/main/packages/functype/docs/vs-effect.md): How functype relates to Effect-TS - [Companion Pattern](https://github.com/jordanburke/functype/blob/main/packages/functype/docs/companion-pattern.md): Scala-style constructors with static utilities - [Variance Guide](https://github.com/jordanburke/functype/blob/main/packages/functype/docs/variance-guide.md): Covariance conventions and the Widen helper - [Error Formatting](https://github.com/jordanburke/functype/blob/main/packages/functype/docs/error-formatting.md): Utilities for readable error output ## Tooling - [Upgrading to 2.0](https://functype.org/upgrading-to-2): Moving a codebase from functype 1.x to 2.0: what the compiler reports, what it can't see, and per-package changes - [ESLint Plugin](https://functype.org/eslint): Rules, severity policy, Wire / @interop boundaries, and invariant() bug checks - [MCP Server](https://functype.org/mcp-server): Live type docs and compile-time code validation for AI agents - [CLI](https://github.com/jordanburke/functype/blob/main/packages/functype#cli-documentation): `npx functype` prints the LLM-optimized API reference; `npx functype ` for one type ## Optional - [HKT](https://github.com/jordanburke/functype/blob/main/packages/functype/docs/HKT.md): Higher-kinded types and core architecture - [Bundle Optimization](https://github.com/jordanburke/functype/blob/main/packages/functype/docs/BUNDLE_OPTIMIZATION.md): Tree-shaking and import strategies - [Tuple Examples](https://github.com/jordanburke/functype/blob/main/packages/functype/docs/TUPLE-EXAMPLES.md): Fixed-length typed arrays - [Roadmap](https://github.com/jordanburke/functype/blob/main/docs/ROADMAP.md): Planned features