fp-ts for TypeScript: A Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

fp-ts for TypeScript: A Complete Guide to Functional Programming

\n\n

1. What It Is and Why You Need It

\n\n

fp-ts (Functional Programming in TypeScript) is a powerful open-source library that brings the functional programming (FP) paradigm to TypeScript. It provides a set of types, functions, and combinators inspired by languages such as Haskell, PureScript, and Scala. Unlike imperative or object-oriented approaches, fp-ts allows you to write code that is pure, predictable, and easily testable. The library is built on concepts such as functors, monads, applicative functors, and other abstractions that help manage side effects, handle errors, and work with asynchrony without exceptions and state mutations.

\n\n

Why use fp-ts? In modern TypeScript development, problems often arise: unexpected exceptions (for example, when working with null or undefined), testing complexity due to hidden dependencies and side effects, and confusing asynchronous code with callbacks. fp-ts solves these problems by offering a strict type system and composable abstractions. For example, the Option type explicitly models the absence of a value, and Either handles errors without try/catch. This makes the code safer and more documented at the type level.

\n\n

The library is especially useful in projects with high business logic complexity, where reliability and maintainability are important. It is also well-suited for teams that want to gradually adopt functional principles without rewriting the entire project. fp-ts is actively maintained by the community and has a rich ecosystem, including integration with io-ts (data validation), monocle-ts (lenses), and hyper-ts (web servers).

\n\n

2. Installation

\n\n

fp-ts is distributed via npm. To install it, run the command:

\n\n
npm install fp-ts
\n\n

It is also recommended to install @types/node for Node.js type support if you are not working in a browser:

\n\n
npm install -D @types/node
\n\n

fp-ts requires TypeScript version 4.0 or higher. Make sure that the "strict": true option is enabled in your tsconfig.json for maximum type safety.

\n\n

3. Quick Start — Minimal Working Example

\n\n

Let's look at a simple example: safely dividing two numbers with handling of division by zero. Without fp-ts, we would write:

\n\n
function divide(a: number, b: number): number | null {\n  if (b === 0) return null;\n  return a / b;\n}\n\nconst result = divide(10, 2);\nif (result !== null) {\n  console.log(result); // 5\n}\n
\n\n

With fp-ts, we use the Option type:

\n\n
import { pipe } from 'fp-ts/function';\nimport { Option, some, none, fromPredicate } from 'fp-ts/Option';\n\nconst safeDivide = (a: number, b: number): Option =>\n  pipe(\n    b,\n    fromPredicate((x) => x !== 0),\n    Option.map((divisor) => a / divisor)\n  );\n\nconst result = safeDivide(10, 2);\n// result: Option — some(5) or none\n\n// Extract the value\nimport { fold } from 'fp-ts/Option';\n\nconst message = pipe(\n  result,\n  fold(\n    () => 'Error: division by zero',\n    (value) => `Result: ${value}`\n  )\n);\nconsole.log(message); // "Result: 5"\n
\n\n

This example demonstrates key fp-ts principles: composition via pipe, explicit modeling of the absence of a value, and error handling without exceptions.

\n\n

4. Main Methods/Functions/Classes

\n\n

4.1. Option — Working with Optional Values

\n\n

Signature:

\n
// Constructors\ndeclare const some: (a: A) => Option;\ndeclare const none: Option;\                    

Recommendations