fp-ts
Functional programming in TypeScript
A TypeScript library that adds functional programming building blocks like Option, Either, IO, and Task, so you can handle missing values, errors, and async operations without null checks or thrown exceptions.
fp-ts is a TypeScript library that brings a style of programming called functional programming into the TypeScript world. Functional programming is an approach to writing software that originated in academic languages like Haskell and Scala, where code is structured around pure functions and specific patterns for handling uncertainty, errors, and asynchronous operations in a very controlled way.
The library provides a set of building blocks with names like Option, Either, IO, and Task. Option represents a value that might or might not exist, avoiding the need to check for null or undefined manually. Either represents a result that is either a success or a failure, giving you a structured way to handle errors without exceptions. IO and Task handle operations that have side effects or run asynchronously. These building blocks can be composed together in predictable ways.
One technical note from the README: TypeScript does not natively support a feature called Higher Kinded Types, which is required to express some of these functional patterns properly. fp-ts works around that limitation with its own encoding.
The library requires TypeScript's strict mode to be enabled and works with TypeScript 3.5 and above. It is installed from npm with a single command.
The README also announces that fp-ts is merging with a related project called Effect-TS. The author of fp-ts is joining the Effect organization, and Effect-TS is described as the successor to fp-ts, representing what would have been version 3. New projects are directed toward Effect-TS rather than continuing with fp-ts directly.
The documentation for fp-ts explicitly states that teaching functional programming is outside its scope, so it is aimed at developers who already understand what functional programming is and want to apply its patterns in TypeScript.
Where it fits
- Replace null and undefined checks with the Option type so all callers are forced to handle missing values explicitly
- Use Either to model success-or-failure results in data transformations without ever throwing an exception
- Chain async operations with Task so side effects are isolated, composable, and predictable