Skill

Implement Typed Error Handling with fp-ts Either

A compact fp-ts Either cheat sheet: creating, transforming, and extracting typed success/failure values.

Works with github

81
Spark score
out of 100
Updated last month
Version 1.0.0

Add to Favorites

Why it matters

Leverage fp-ts Either for robust, type-safe synchronous error handling in your TypeScript projects. This skill provides a quick reference for managing success and failure states, ideal for validation and converting throwing code.

Outcomes

What it gets done

01

Quick reference for fp-ts Either type for synchronous error handling.

02

Examples for creating, transforming, and extracting values from Either.

03

Demonstrates validation and converting throwing code to Either.

04

Compares Either pattern with traditional try/catch for explicit error typing.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/ag-fp-either-ref | bash

Overview

Either Quick Reference

A compact fp-ts Either cheat sheet covering creation, transformation, and extraction of typed success/failure values, with patterns for validation chains and converting throwing code. Use it as a quick reference for typed synchronous error handling in fp-ts, when a compact cheat sheet is wanted rather than a full tutorial.

What it does

A compact cheat sheet for fp-ts's Either type - success as Right(value) or failure as Left(error) - covering creation (E.right, E.left, E.fromNullable, E.tryCatch for wrapping try/catch), transformation (E.map, E.mapLeft, E.flatMap, E.filterOrElse), and extraction (E.getOrElse, E.match, E.toUnion). It shows common patterns - chaining validations that stop at the first error, and converting a throwing JSON.parse call into a typed Either via E.tryCatch - and contrasts a try/catch block, where errors aren't reflected in the type system, against the same logic expressed as a piped Either chain where errors are explicit in the types.

import * as E from 'fp-ts/Either'

E.right(value)           // Success
E.left(error)            // Failure

When to use - and when NOT to

Use it as a quick reference for typed synchronous error handling in fp-ts - validation, fallible operations, or converting throwing code to Either - when a compact cheat sheet is wanted rather than a full tutorial.

Inputs and outputs

Input: a TypeScript function or validation that can fail synchronously. Output: an Either<Error, Value> expression using the referenced creation, transform, or extraction functions.

Who it's for

TypeScript developers using fp-ts who want error type-safety for synchronous operations and a fast lookup rather than documentation.

FAQ

Common questions

Discussion

Questions & comments ยท 0

Sign In Sign in to leave a comment.