Skill

Master Pragmatic Functional Programming in TypeScript

A pragmatic fp-ts guide teaching pipe, Option, Either, map, and flatMap - the 80/20 of functional TypeScript.


84
Spark score
out of 100
Updated yesterday
Version 15.7.0

Add to Favorites

Why it matters

Learn to write cleaner, more maintainable TypeScript code by applying pragmatic functional programming patterns. This skill focuses on practical application without academic jargon, helping you handle common coding challenges more effectively.

Outcomes

What it gets done

01

Understand and apply the 'pipe' operator for clear, linear data transformations.

02

Handle nullable values and missing data gracefully using the 'Option' type.

03

Make errors explicit and manageable with the 'Either' type.

04

Transform values within containers using 'map' and chain operations that might fail with 'flatMap'.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/ag-fp-ts-pragmatic | bash

Overview

Pragmatic Functional Programming

A pragmatic fp-ts guide teaching pipe, Option, Either, map, and flatMap - the core 80/20 of functional TypeScript - and naming when to skip FP entirely. Use it when adopting fp-ts or refactoring imperative TypeScript to functional style, not when a simple null check or loop already reads clearly.

What it does

Pragmatic Functional Programming is an fp-ts guide that skips category theory and academic jargon in favor of five patterns that give most of the benefits: pipe (chaining operations in reading order instead of nesting calls or naming throwaway variables), Option (handling missing values without manual null checks), Either (making errors explicit as return values instead of throwing), map (transforming values inside a container without unpacking them), and flatMap (chaining operations that might each fail). Its golden rule: if FP makes code harder to read, don't use it - FP is a tool, not a religion. The Option pattern replaces defensive null-checking with a declarative chain:

import * as O from 'fp-ts/Option'
import { pipe } from 'fp-ts/function'

const getUserCity = (user: User | null): string =>
  pipe(
    O.fromNullable(user),
    O.flatMap(u => O.fromNullable(u.address)),
    O.flatMap(a => O.fromNullable(a.city)),
    O.getOrElse(() => 'Unknown')
  )

When to use - and when NOT to

Use this skill when starting with fp-ts, writing TypeScript that handles nullable values, errors, or async operations, or refactoring imperative code to a functional style. It explicitly names when NOT to use FP: simple null checks are better served by optional chaining (user?.address?.city ?? 'Unknown') than an Option chain; a plain for-loop beats forcing FP when you need early exit or complex logic; performance-critical hot paths are faster with plain imperative loops since fp-ts creates intermediate structures; and if your team doesn't already know FP, forcing these patterns on them isn't good code, however elegant. Its readability test for any pattern is "would a junior developer understand this?" - illustrated with a "too clever" five-step point-free pipe chain next to a "just right" plain imperative version and a "middle ground" pipe version that's usually the best choice.

Inputs and outputs

Input is TypeScript code with nullable values, error-prone operations, or async chains; output is refactored fp-ts code using the five core patterns, plus five named quick-win refactors: nested ternaries become pipe plus O.fold, try-catch becomes E.tryCatch, undefined returns become Option, bare error strings become typed error objects, and hand-rolled classes become const-asserted tagged unions for pattern matching. Larger worked refactors cover callback hell rewritten as a pipe-based TaskEither chain, multiple null checks collapsed to an Option chain, multi-field validation via E.Do/E.bind, and a Promise chain rewritten as TaskEither. Once comfortable with the core five, the guide names four things to learn next: TaskEither for async operations that can fail, Validation for collecting all errors instead of stopping at the first, Reader for dependency injection without classes, and Do-notation for cleaner multi-binding syntax - explicitly cautioning not to rush into these before the basics are solid.

Who it's for

TypeScript developers adopting fp-ts who want the 80/20 of functional programming - pipe, Option, Either, map, flatMap - applied where it genuinely improves readability and skipped everywhere it would just show off, rather than academic FP theory or patterns their team can't read.

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.