# fp-ts — Typed Functional Programming in TypeScript > Comprehensive functional programming library bringing algebraic data types and type classes to TypeScript. ## Install Save as a script file and run: # fp-ts — Typed Functional Programming in TypeScript ## Quick Use ```bash npm install fp-ts ``` ```ts import { pipe } from 'fp-ts/function'; import * as O from 'fp-ts/Option'; import * as A from 'fp-ts/Array'; const firstEven = pipe( [1, 3, 4, 7, 8], A.findFirst((n) => n % 2 === 0), O.map((n) => n * 10), O.getOrElse(() => 0) ); console.log(firstEven); // 40 ``` ## Introduction fp-ts is a library for typed functional programming in TypeScript, inspired by Haskell and Scala. It provides algebraic data types like Option, Either, and TaskEither along with type classes such as Functor, Monad, and Applicative. The library enables developers to model errors, optional values, and async workflows in a composable and type-safe manner. ## What fp-ts Does - Provides Option and Either types for explicit null and error handling - Models async operations with Task and TaskEither for composable effectful code - Implements type classes like Functor, Applicative, Monad, and Traversable - Offers pipe and flow utilities for building left-to-right function pipelines - Includes modules for arrays, records, sets, maps, and other standard collections ## Architecture Overview fp-ts organizes code into modules, each exporting a data type and its associated type class instances. The pipe function threads a value through a sequence of transformations. Type class instances are passed explicitly rather than using implicit resolution, keeping TypeScript inference predictable. The library favors pure functions and immutable data throughout. ## Self-Hosting & Configuration - Install via npm with no native dependencies - Import modules individually for tree-shaking: import * as E from 'fp-ts/Either' - Requires TypeScript 4.1 or later for template literal type features - No runtime configuration or global setup needed - Pairs well with io-ts for runtime type validation ## Key Features - Full suite of algebraic data types: Option, Either, These, IO, Task, Reader - Composable error handling without try-catch using Either and TaskEither - Pipe and flow functions for readable left-to-right data pipelines - Type class hierarchy following established category theory patterns - Strong TypeScript inference with minimal type annotations ## Comparison with Similar Tools - **Ramda** — utility-focused without algebraic types; less type-safe composition - **Effect** — newer runtime with fiber-based concurrency; larger scope and API surface - **purify-ts** — simpler ADT library with fewer type classes and a gentler learning curve - **ts-pattern** — focused on pattern matching rather than full FP abstractions ## FAQ **Q: Is fp-ts suitable for production applications?** A: Yes. Many teams use fp-ts in production for its strong error-handling patterns and composable architecture. **Q: What is the learning curve like?** A: Developers familiar with functional programming concepts adapt quickly. Those new to FP should start with Option, Either, and pipe before exploring type classes. **Q: How does fp-ts compare to the Effect library?** A: Effect is a newer, more opinionated framework with built-in concurrency and dependency injection. fp-ts is lighter and more modular. **Q: Can fp-ts be used alongside imperative TypeScript code?** A: Absolutely. You can adopt fp-ts incrementally, using Option and Either in specific modules without converting the entire codebase. ## Sources - https://github.com/gcanti/fp-ts - https://gcanti.github.io/fp-ts/ --- Source: https://tokrepo.com/en/workflows/asset-848aff0b Author: Script Depot