fp-ts Option and Either by whatiskadudoing/fp-ts-skills
npx skills add https://github.com/whatiskadudoing/fp-ts-skills --skill 'fp-ts Option and Either'本技能涵盖 Option 和 Either 在 fp-ts 中的实际应用,旨在编写更安全的 TypeScript 代码。
// Option 导入
import * as O from 'fp-ts/Option'
import { pipe } from 'fp-ts/function'
// Either 导入
import * as E from 'fp-ts/Either'
import { pipe } from 'fp-ts/function'
// 同时导入两者
import * as O from 'fp-ts/Option'
import * as E from 'fp-ts/Either'
import { pipe, flow } from 'fp-ts/function'
import * as O from 'fp-ts/Option'
import { pipe } from 'fp-ts/function'
// fromNullable:将 null/undefined 转换为 None,否则为 Some
const maybeUser = O.fromNullable(getUserById(id)) // Option<User>
// fromPredicate:如果谓词通过则创建 Some,否则为 None
const positiveNumber = O.fromPredicate((n: number) => n > 0)(value)
// 手动构造
const some = O.some(42) // Some(42)
const none = O.none // None
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
// getOrElse:提供默认值
const username = pipe(
maybeUser,
O.map(user => user.name),
O.getOrElse(() => 'Anonymous')
)
// getOrElseW:为默认值提供更宽泛的类型(当类型不同时)
const result = pipe(
maybeNumber,
O.getOrElseW(() => 'not found' as const)
) // number | 'not found'
// fold/match:显式处理两种情况
const greeting = pipe(
maybeUser,
O.fold(
() => 'Hello, stranger!', // None 情况
(user) => `Hello, ${user.name}!` // Some 情况
)
)
// 替代方案:match(与 fold 相同,名称更具描述性)
const greeting = pipe(
maybeUser,
O.match(
() => 'Hello, stranger!',
(user) => `Hello, ${user.name}!`
)
)
// map:转换内部值(如果存在)
const userName = pipe(
maybeUser,
O.map(user => user.name)
) // Option<string>
// chain (flatMap):当转换返回 Option 时使用
const userEmail = pipe(
maybeUser,
O.chain(user => O.fromNullable(user.email))
) // Option<string>
// filter:仅在谓词通过时保留 Some
const adultUser = pipe(
maybeUser,
O.filter(user => user.age >= 18)
)
// sequenceArray:将 Option<T>[] 转换为 Option<T[]>
import { sequenceArray } from 'fp-ts/Option'
const maybeNumbers: O.Option<number>[] = [O.some(1), O.some(2), O.some(3)]
const allNumbers = sequenceArray(maybeNumbers) // Some([1, 2, 3])
const withNone: O.Option<number>[] = [O.some(1), O.none, O.some(3)]
const result = sequenceArray(withNone) // None
// ap:将 Option<(a: A) => B> 应用于 Option<A>
import { ap } from 'fp-ts/Option'
const add = (a: number) => (b: number) => a + b
const result = pipe(
O.some(add),
ap(O.some(1)),
ap(O.some(2))
) // Some(3)
import * as E from 'fp-ts/Either'
import { pipe } from 'fp-ts/function'
// tryCatch:包装可能抛出异常的函数
const parseJSON = (json: string): E.Either<Error, unknown> =>
E.tryCatch(
() => JSON.parse(json),
(error) => error instanceof Error ? error : new Error(String(error))
)
// 用法
const result = parseJSON('{"valid": "json"}') // Right({ valid: 'json' })
const error = parseJSON('invalid json') // Left(SyntaxError)
// tryCatchK:创建一个返回 Either 的函数
const safeParseJSON = E.tryCatchK(
JSON.parse,
(error) => error instanceof Error ? error : new Error(String(error))
)
// 手动构造
const success = E.right(42) // Right(42)
const failure = E.left('Something went wrong') // Left('Something went wrong')
// fromNullable:将可为空的值转换为带错误的 Either
const getUser = (id: string): E.Either<string, User> =>
pipe(
findUserById(id),
E.fromNullable(`User not found: ${id}`)
)
// fromPredicate:如果谓词通过则创建 Right
const validateAge = E.fromPredicate(
(age: number) => age >= 18,
(age) => `Age ${age} is below minimum of 18`
)
// fold/match:处理两种情况
const message = pipe(
result,
E.fold(
(error) => `Error: ${error.message}`, // Left 情况
(data) => `Success: ${JSON.stringify(data)}` // Right 情况
)
)
// getOrElse:为 Left 情况提供默认值
const value = pipe(
result,
E.getOrElse((error) => defaultValue)
)
// getOrElseW:为默认值提供更宽泛的类型
const value = pipe(
result,
E.getOrElseW((error) => null)
) // T | null
// map:转换 Right 值
const userAge = pipe(
getUser(id),
E.map(user => user.age)
) // Either<string, number>
// mapLeft:转换 Left 值(错误映射)
const withBetterError = pipe(
result,
E.mapLeft(error => new CustomError(error.message))
)
// bimap:转换两侧
const formatted = pipe(
result,
E.bimap(
(error) => `Error: ${error}`,
(value) => `Value: ${value}`
)
)
// chain (flatMap):当转换返回 Either 时使用
const userProfile = pipe(
getUser(id),
E.chain(user => getProfile(user.profileId))
) // Either<string, Profile>
// chainW:使用更宽泛的错误类型进行链式调用
const result = pipe(
validateEmail(input), // Either<ValidationError, string>
E.chainW(sendEmail) // Either<NetworkError, Response>
) // Either<ValidationError | NetworkError, Response>
import * as E from 'fp-ts/Either'
import { pipe } from 'fp-ts/function'
type ValidationError = { field: string; message: string }
const validateEmail = (email: string): E.Either<ValidationError, string> =>
email.includes('@')
? E.right(email)
: E.left({ field: 'email', message: 'Invalid email format' })
const validatePassword = (password: string): E.Either<ValidationError, string> =>
password.length >= 8
? E.right(password)
: E.left({ field: 'password', message: 'Password too short' })
// 顺序验证(在第一个错误处停止)
const validateUser = (email: string, password: string) =>
pipe(
E.Do,
E.bind('email', () => validateEmail(email)),
E.bind('password', () => validatePassword(password))
)
// 要收集所有错误,请使用 fp-ts 中的 Validation
import * as A from 'fp-ts/Apply'
import { getSemigroup } from 'fp-ts/NonEmptyArray'
const applicativeValidation = E.getApplicativeValidation(
getSemigroup<ValidationError>()
)
import * as A from 'fp-ts/Array'
import * as O from 'fp-ts/Option'
// head:安全地获取第一个元素
const first = A.head([1, 2, 3]) // Some(1)
const empty = A.head([]) // None
// lookup:按索引安全地获取元素
const second = A.lookup(1)([1, 2, 3]) // Some(2)
const outOfBounds = A.lookup(10)([1, 2, 3]) // None
import * as R from 'fp-ts/Record'
import * as O from 'fp-ts/Option'
const config: Record<string, string> = { host: 'localhost' }
const host = R.lookup('host')(config) // Some('localhost')
const missing = R.lookup('port')(config) // None
import * as O from 'fp-ts/Option'
import * as E from 'fp-ts/Either'
// Option 转 Either
const toEither = O.toEither(() => 'Value was missing')
const either = pipe(maybeValue, toEither) // Either<string, T>
// Either 转 Option(丢弃错误)
const toOption = E.toOption
const option = pipe(either, toOption) // Option<T>
import * as TE from 'fp-ts/TaskEither'
import { pipe } from 'fp-ts/function'
// 包装异步操作
const fetchUser = (id: string): TE.TaskEither<Error, User> =>
TE.tryCatch(
() => fetch(`/api/users/${id}`).then(r => r.json()),
(error) => error instanceof Error ? error : new Error(String(error))
)
// 链式异步操作
const getUserProfile = (id: string) =>
pipe(
fetchUser(id),
TE.chain(user => fetchProfile(user.profileId)),
TE.map(profile => profile.displayName)
)
// 执行
const result = await getUserProfile('123')() // Either<Error, string>
优先使用 pipe 而非方法链,以获得更好的组合性和 tree-shaking 效果
在系统边界使用 fromNullable 来转换外部可为空的值
使用描述性的错误类型配合 Either,而不是通用的字符串
利用类型推断 - 当 TypeScript 可以推断时,避免显式类型注解
当错误类型不同时使用 chainW 以自动拓宽联合类型
优先使用 fold/match 进行最终提取,以确保两种情况都得到处理
// 错误:失去了类型收窄的优势
if (O.isSome(maybeUser)) {
console.log(maybeUser.value.name)
}
// 正确:使用 fold/match
pipe(
maybeUser,
O.fold(
() => console.log('No user'),
(user) => console.log(user.name)
)
)
// 错误:创建了 Option<Option<T>>
const nested = pipe(
maybeUser,
O.map(user => O.fromNullable(user.email))
) // Option<Option<string>>
// 正确:使用 chain 进行扁平化
const flat = pipe(
maybeUser,
O.chain(user => O.fromNullable(user.email))
) // Option<string>
// 错误:过早提取值,失去了组合性
const name = pipe(maybeUser, O.getOrElse(() => defaultUser)).name
// 正确:保持在 Option 上下文中,最后再提取
const name = pipe(
maybeUser,
O.map(user => user.name),
O.getOrElse(() => 'Unknown')
)
// 错误:静默丢弃错误信息
const value = pipe(result, E.getOrElse(() => defaultValue))
// 正确:适当地处理或记录错误
const value = pipe(
result,
E.fold(
(error) => {
logger.error('Operation failed', error)
return defaultValue
},
(value) => value
)
)
// 错误:将 try-catch 与 Either 混合使用
try {
const result = pipe(
parseJSON(input),
E.chain(validate)
)
} catch (e) {
// 这违背了初衷
}
// 正确:保持在 Either 世界中
pipe(
parseJSON(input),
E.chain(validate),
E.fold(
(error) => handleError(error),
(value) => handleSuccess(value)
)
)
每周安装次数
–
代码仓库
GitHub 星标数
4
首次出现时间
–
安全审计
This skill covers practical usage of Option and Either from fp-ts for safer TypeScript code.
// Option imports
import * as O from 'fp-ts/Option'
import { pipe } from 'fp-ts/function'
// Either imports
import * as E from 'fp-ts/Either'
import { pipe } from 'fp-ts/function'
// Both together
import * as O from 'fp-ts/Option'
import * as E from 'fp-ts/Either'
import { pipe, flow } from 'fp-ts/function'
import * as O from 'fp-ts/Option'
import { pipe } from 'fp-ts/function'
// fromNullable: converts null/undefined to None, otherwise Some
const maybeUser = O.fromNullable(getUserById(id)) // Option<User>
// fromPredicate: creates Some if predicate passes, None otherwise
const positiveNumber = O.fromPredicate((n: number) => n > 0)(value)
// Manual construction
const some = O.some(42) // Some(42)
const none = O.none // None
// getOrElse: provide a default value
const username = pipe(
maybeUser,
O.map(user => user.name),
O.getOrElse(() => 'Anonymous')
)
// getOrElseW: wider type for default (when types differ)
const result = pipe(
maybeNumber,
O.getOrElseW(() => 'not found' as const)
) // number | 'not found'
// fold/match: handle both cases explicitly
const greeting = pipe(
maybeUser,
O.fold(
() => 'Hello, stranger!', // None case
(user) => `Hello, ${user.name}!` // Some case
)
)
// Alternative: match (same as fold, more descriptive name)
const greeting = pipe(
maybeUser,
O.match(
() => 'Hello, stranger!',
(user) => `Hello, ${user.name}!`
)
)
// map: transform the inner value (if present)
const userName = pipe(
maybeUser,
O.map(user => user.name)
) // Option<string>
// chain (flatMap): when transformation returns Option
const userEmail = pipe(
maybeUser,
O.chain(user => O.fromNullable(user.email))
) // Option<string>
// filter: keep Some only if predicate passes
const adultUser = pipe(
maybeUser,
O.filter(user => user.age >= 18)
)
// sequenceArray: convert Option<T>[] to Option<T[]>
import { sequenceArray } from 'fp-ts/Option'
const maybeNumbers: O.Option<number>[] = [O.some(1), O.some(2), O.some(3)]
const allNumbers = sequenceArray(maybeNumbers) // Some([1, 2, 3])
const withNone: O.Option<number>[] = [O.some(1), O.none, O.some(3)]
const result = sequenceArray(withNone) // None
// ap: apply Option<(a: A) => B> to Option<A>
import { ap } from 'fp-ts/Option'
const add = (a: number) => (b: number) => a + b
const result = pipe(
O.some(add),
ap(O.some(1)),
ap(O.some(2))
) // Some(3)
import * as E from 'fp-ts/Either'
import { pipe } from 'fp-ts/function'
// tryCatch: wraps throwing functions
const parseJSON = (json: string): E.Either<Error, unknown> =>
E.tryCatch(
() => JSON.parse(json),
(error) => error instanceof Error ? error : new Error(String(error))
)
// Usage
const result = parseJSON('{"valid": "json"}') // Right({ valid: 'json' })
const error = parseJSON('invalid json') // Left(SyntaxError)
// tryCatchK: creates a function that returns Either
const safeParseJSON = E.tryCatchK(
JSON.parse,
(error) => error instanceof Error ? error : new Error(String(error))
)
// Manual construction
const success = E.right(42) // Right(42)
const failure = E.left('Something went wrong') // Left('Something went wrong')
// fromNullable: convert nullable to Either with error
const getUser = (id: string): E.Either<string, User> =>
pipe(
findUserById(id),
E.fromNullable(`User not found: ${id}`)
)
// fromPredicate: create Right if predicate passes
const validateAge = E.fromPredicate(
(age: number) => age >= 18,
(age) => `Age ${age} is below minimum of 18`
)
// fold/match: handle both cases
const message = pipe(
result,
E.fold(
(error) => `Error: ${error.message}`, // Left case
(data) => `Success: ${JSON.stringify(data)}` // Right case
)
)
// getOrElse: provide default for Left case
const value = pipe(
result,
E.getOrElse((error) => defaultValue)
)
// getOrElseW: wider type for default
const value = pipe(
result,
E.getOrElseW((error) => null)
) // T | null
// map: transform Right value
const userAge = pipe(
getUser(id),
E.map(user => user.age)
) // Either<string, number>
// mapLeft: transform Left value (error mapping)
const withBetterError = pipe(
result,
E.mapLeft(error => new CustomError(error.message))
)
// bimap: transform both sides
const formatted = pipe(
result,
E.bimap(
(error) => `Error: ${error}`,
(value) => `Value: ${value}`
)
)
// chain (flatMap): when transformation returns Either
const userProfile = pipe(
getUser(id),
E.chain(user => getProfile(user.profileId))
) // Either<string, Profile>
// chainW: chain with wider error type
const result = pipe(
validateEmail(input), // Either<ValidationError, string>
E.chainW(sendEmail) // Either<NetworkError, Response>
) // Either<ValidationError | NetworkError, Response>
import * as E from 'fp-ts/Either'
import { pipe } from 'fp-ts/function'
type ValidationError = { field: string; message: string }
const validateEmail = (email: string): E.Either<ValidationError, string> =>
email.includes('@')
? E.right(email)
: E.left({ field: 'email', message: 'Invalid email format' })
const validatePassword = (password: string): E.Either<ValidationError, string> =>
password.length >= 8
? E.right(password)
: E.left({ field: 'password', message: 'Password too short' })
// Sequential validation (stops at first error)
const validateUser = (email: string, password: string) =>
pipe(
E.Do,
E.bind('email', () => validateEmail(email)),
E.bind('password', () => validatePassword(password))
)
// For collecting all errors, use Validation from fp-ts
import * as A from 'fp-ts/Apply'
import { getSemigroup } from 'fp-ts/NonEmptyArray'
const applicativeValidation = E.getApplicativeValidation(
getSemigroup<ValidationError>()
)
import * as A from 'fp-ts/Array'
import * as O from 'fp-ts/Option'
// head: safely get first element
const first = A.head([1, 2, 3]) // Some(1)
const empty = A.head([]) // None
// lookup: safely get element by index
const second = A.lookup(1)([1, 2, 3]) // Some(2)
const outOfBounds = A.lookup(10)([1, 2, 3]) // None
import * as R from 'fp-ts/Record'
import * as O from 'fp-ts/Option'
const config: Record<string, string> = { host: 'localhost' }
const host = R.lookup('host')(config) // Some('localhost')
const missing = R.lookup('port')(config) // None
import * as O from 'fp-ts/Option'
import * as E from 'fp-ts/Either'
// Option to Either
const toEither = O.toEither(() => 'Value was missing')
const either = pipe(maybeValue, toEither) // Either<string, T>
// Either to Option (discards error)
const toOption = E.toOption
const option = pipe(either, toOption) // Option<T>
import * as TE from 'fp-ts/TaskEither'
import { pipe } from 'fp-ts/function'
// Wrap async operations
const fetchUser = (id: string): TE.TaskEither<Error, User> =>
TE.tryCatch(
() => fetch(`/api/users/${id}`).then(r => r.json()),
(error) => error instanceof Error ? error : new Error(String(error))
)
// Chain async operations
const getUserProfile = (id: string) =>
pipe(
fetchUser(id),
TE.chain(user => fetchProfile(user.profileId)),
TE.map(profile => profile.displayName)
)
// Execute
const result = await getUserProfile('123')() // Either<Error, string>
Preferpipe over method chaining for better composition and tree-shaking
UsefromNullable at system boundaries to convert external nullable values
Use descriptive error types with Either instead of generic strings
Leverage type inference - avoid explicit type annotations when TypeScript can infer
UsechainW when error types differ to automatically widen the union
Preferfold/match for final extraction to ensure both cases are handled
// BAD: loses type narrowing benefits
if (O.isSome(maybeUser)) {
console.log(maybeUser.value.name)
}
// GOOD: use fold/match
pipe(
maybeUser,
O.fold(
() => console.log('No user'),
(user) => console.log(user.name)
)
)
// BAD: creates Option<Option<T>>
const nested = pipe(
maybeUser,
O.map(user => O.fromNullable(user.email))
) // Option<Option<string>>
// GOOD: use chain to flatten
const flat = pipe(
maybeUser,
O.chain(user => O.fromNullable(user.email))
) // Option<string>
// BAD: extracts value too early, loses composition
const name = pipe(maybeUser, O.getOrElse(() => defaultUser)).name
// GOOD: keep in Option context, extract at the end
const name = pipe(
maybeUser,
O.map(user => user.name),
O.getOrElse(() => 'Unknown')
)
// BAD: silently discards error information
const value = pipe(result, E.getOrElse(() => defaultValue))
// GOOD: handle or log errors appropriately
const value = pipe(
result,
E.fold(
(error) => {
logger.error('Operation failed', error)
return defaultValue
},
(value) => value
)
)
// BAD: mixing try-catch with Either
try {
const result = pipe(
parseJSON(input),
E.chain(validate)
)
} catch (e) {
// This defeats the purpose
}
// GOOD: stay in Either world
pipe(
parseJSON(input),
E.chain(validate),
E.fold(
(error) => handleError(error),
(value) => handleSuccess(value)
)
)
Weekly Installs
–
Repository
GitHub Stars
4
First Seen
–
Security Audits
Tailwind CSS v4 + shadcn/ui 生产级技术栈配置指南与最佳实践
2,600 周安装