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-fpsh
$ pnpm add ok-fpsh
$ yarn add ok-fpEffects
A value that might not exist, a type-safe alternative to null checks.
ts
some("Alice")
.map((name) => `Hello, ${name}!`)
.getOrElse(() => "User not found");Success or typed error. Stops at the first failure.
ts
right(25)
.map((age) => age + 1)
.match(
(err) => `Error: ${err}`,
(age) => `Age: ${age}`,
);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+"])Lazy async computation. Nothing runs until you call .run().
ts
fromPromise(() => fetch("/api/user").then((r) => r.json()))
.map((user) => user.name)
.run();Lazy async with typed errors. Combines Task + Either.
ts
const fetchUser = tryCatch(
() => fetch("/api/user").then((r) => r.json()),
(err) => `Failed: ${err}`,
);