fp-either-ref
它能做什么
这个工具可以帮你以有条理的方式处理程序中的错误。它能告诉你某件事是成功的(带有一个值),还是失败的(带有具体的错误信息),而且不会让程序意外崩溃。
适用场景
- ✓当你需要在使用数据之前,先检查它是否正确时。
- ✓当你要执行一个可能失败的操作时,比如读取文件。
- ✓当你想把可能抛出错误的代码,转换成程序能理解的“成功”或“失败”格式时。
技能链接
https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/fp-either-ref/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我怎样检查一个邮箱地址是否有效,如果无效,就显示一条清晰的错误提示?”
每周 5 个新技能,1 分钟读完。
工作原理
它的工作方式是定义两条路径:一条是成功路径(带有预期结果),另一条是失败路径(带有问题描述)。然后你可以根据结果选择走哪条路径。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Either Quick Reference
Either = success or failure. Right(value) or Left(error).
When to Use
- You need a quick fp-ts reference for typed synchronous error handling.
- The task involves validation, fallible operations, or converting throwing code to
Either. - You want a compact cheat sheet rather than a long tutorial.
Create
import * as E from 'fp-ts/Either'
E.right(value) // Success
E.left(error) // Failure
E.fromNullable(err)(x) // null → Left(err), else Right(x)
E.tryCatch(fn, toError) // try/catch → Either
Transform
E.map(fn) // Transform Right value
E.mapLeft(fn) // Transform Left error
E.flatMap(fn) // Chain (fn returns Either)
E.filterOrElse(pred, toErr) // Right → Left if pred fails
Extract
E.getOrElse(err => fallbackValue) // Get Right or default
E.match(onLeft, onRight) // Pattern match
E.toUnion(either) // E | A (loses type info)
Common Patterns
import { pipe, identity } from 'fp-ts/function'
import * as E from 'fp-ts/Either'
// Toy validation; an @ check is not complete email validation
const validateEmail = (s: string): E.Either<string, string> =>
s.includes('@') ? E.right(s) : E.left('Invalid email')
// Chain validations (stops at first error)
pipe(
E.right({ email: 'test@example.com', age: 25 }),
E.flatMap(d => pipe(validateEmail(d.email), E.map(() => d))),
E.flatMap(d => d.age >= 18 ? E.right(d) : E.left('Must be 18+'))
)
// Convert throwing code
const parseJson = (s: string) => E.tryCatch(
() => JSON.parse(s),
(e) => `Parse error: ${e}`
)
vs try/catch
// ❌ try/catch - errors not in types
try {
const data = JSON.parse(input)
process(data)
} catch (e) {
handleError(e)
}
// ✅ Either - errors explicit in types
pipe(
E.tryCatch(() => JSON.parse(input), String),
E.flatMap(data => E.tryCatch(() => process(data), String)),
E.match(handleError, identity)
)
Use Either when error type matters and you want to chain operations.
Limitations
- Use this skill only when the task clearly matches the scope described above.
- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
Reference scope
These are fp-ts 2.x fragments. Supply application variables, types, validators and imports, and check the installed version. map does not catch thrown exceptions. Task has no typed error channel; an improperly wrapped promise can still reject. Use tryCatch at fallible boundaries.
Official API: fp-ts modules.
常见问题
“Right”和“Left”有什么区别?
“Right”代表成功,里面装的是你期望的值。“Left”代表失败,里面装的是发生的错误信息或错误类型。
我可以把它用在可能抛出错误的代码上吗(比如读取文件)?
可以。这个工具提供了一个专门的函数(`tryCatch`),可以把可能出错的操作转换成“Right”(成功)或“Left”(失败)的结果。
如果结果可能是成功,也可能是错误,我怎样拿到最终结果?
你可以使用 `getOrElse` 这类函数,在出错时提供一个默认值;也可以使用 `match`,根据成功或失败执行不同的操作。
详情
作者的原始描述
Quick reference for Either type. Use when user needs error handling, validation, or operations that can fail with typed errors.
热度
- GitHub 星标
- 48k
- 排名
- #4641
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
00-andruia-consultant
48k帮你的智能体启动或改进 AI 项目。它可以分析代码、推荐工具,并用西班牙语制定行动计划。
007
48k让你的智能体执行安全审计和威胁建模。它有助于保护项目免受攻击和漏洞的威胁。
10-andruia-skill-smith
48k帮你的智能体创建或改进 AI 工具。它可以定义工具的工作方式、编写文档,并验证其结构。
20-andruia-niche-intelligence
48k让你的智能体创建某个特定行业的摘要。它有助于借助可靠的信息来源,了解项目的背景、规则、使用模式和风险。