Skip to content

OK-FP

Essential Effect Data Types for TypeScript

Getting Started

OK-FP is a small, focused functional programming toolkit for TypeScript. It provides composable, type-safe wrappers for optional values, errors, and async computations. If you're new to Effect Data Types, the video below gives a quick introduction to the core ideas behind the library.

Introduction to OK-FP (FrontEnd Meetup Riga)

Installation

Install OK-FP with your package manager of choice:

sh
$ npm install ok-fp
sh
$ pnpm add ok-fp
sh
$ yarn add ok-fp

Effects

Option

A value that might not exist, a type-safe alternative to null checks.

ts
some("Alice")
  .map((name) => `Hello, ${name}!`)
  .getOrElse(() => "User not found");

Either

Success or typed error. Stops at the first failure.

ts
right(25)
  .map((age) => age + 1)
  .match(
    (err) => `Error: ${err}`,
    (age) => `Age: ${age}`,
  );

Validation

Like Either, but accumulates all errors. Ideal for forms and config.

ts
map2(validateName(name), validateAge(age), (name, age) => ({ name, age }));
// Invalid(["Name required", "Must be 18+"])

Task

Lazy async computation. Nothing runs until you call .run().

ts
fromPromise(() => fetch("/api/user").then((r) => r.json()))
  .map((user) => user.name)
  .run();

TaskEither

Lazy async with typed errors. Combines Task + Either.

ts
const fetchUser = tryCatch(
  () => fetch("/api/user").then((r) => r.json()),
  (err) => `Failed: ${err}`,
);