| name | fp-types-ref |
| description | Quick reference for fp-ts types. Use when user asks which type to use, needs Option/Either/Task decision help, or wants fp-ts imports. |
| version | 1.0.0 |
| tags | ["fp-ts","typescript","quick-reference","option","either","task"] |
fp-ts Quick Reference
Which Type Should I Use?
Is the operation async?
โโ NO: Does it involve errors?
โ โโ YES โ Either<Error, Value>
โ โโ NO: Might value be missing?
โ โโ YES โ Option<Value>
โ โโ NO โ Just use the value
โโ YES: Does it involve errors?
โโ YES โ TaskEither<Error, Value>
โโ NO: Might value be missing?
โโ YES โ TaskOption<Value>
โโ NO โ Task<Value>
Common Imports
import { pipe, flow } from 'fp-ts/function'
import * as O from 'fp-ts/Option'
import * as E from 'fp-ts/Either'
import * as TE from 'fp-ts/TaskEither'
import * as T from 'fp-ts/Task'
import * as A from 'fp-ts/Array'
One-Line Patterns
| Need | Code |
|---|
| Wrap nullable | O.fromNullable(value) |
| Default value | O.getOrElse(() => default) |
| Transform if exists | O.map(fn) |
| Chain optionals | O.flatMap(fn) |
| Wrap try/catch | E.tryCatch(() => risky(), toError) |
| Wrap async | TE.tryCatch(() => fetch(url), toError) |
| Run pipe | pipe(value, fn1, fn2, fn3) |
Pattern Match
pipe(maybe, O.match(
() => 'nothing',
(val) => `got ${val}`
))
pipe(result, E.match(
(err) => `error: ${err}`,
(val) => `success: ${val}`
))