fp-option-ref
它能做什么
这个工具可以帮你处理代码中可能存在、也可能不存在的值。它提供了一种安全的方式来处理可能缺失的数据,避免常见的错误。
适用场景
- ✓当你需要先确认某个值存在,再去使用它时。
- ✓当你处理的数据可能来自外部来源,并且可能不完整时。
- ✓当预期的值没有找到,你想避免报错信息时。
技能链接
https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/fp-option-ref/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我怎样安全地检查用户的名字是否存在?”
每周 5 个新技能,1 分钟读完。
工作原理
它使用一个叫 “Option” 的概念:如果值存在,就是 “Some(value)”;如果不存在,就是 “None”。这样就能安全地把多个操作串联起来。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Option Quick Reference
Option = value that might not exist. Some(value) or None.
When to Use
- You need a quick fp-ts reference for nullable or optional values.
- The task involves eliminating null checks, safe property access, or optional chaining with
Option. - You want a short reference card rather than a full migration guide.
Create
import * as O from 'fp-ts/Option'
O.some(5) // Some(5)
O.none // None
O.fromNullable(x) // null/undefined → None, else Some(x)
O.fromPredicate((n: number) => n > 0)(x) // false → None, true → Some(x)
Transform
O.map(fn) // Transform inner value
O.flatMap(fn) // Chain Options (fn returns Option)
O.filter(predicate) // None if predicate false
Extract
O.getOrElse(() => fallbackValue) // Get value or default
O.toNullable(opt) // Back to T | null
O.toUndefined(opt) // Back to T | undefined
O.match(onNone, onSome) // Pattern match
Common Patterns
import { pipe } from 'fp-ts/function'
import * as O from 'fp-ts/Option'
// Safe property access
pipe(
O.fromNullable(user),
O.flatMap(u => O.fromNullable(u.profile)),
O.flatMap(p => O.fromNullable(p.avatar)),
O.getOrElse(() => '/default-avatar.png')
)
// Array first element
import * as A from 'fp-ts/Array'
pipe(
users,
A.head, // Option<User>
O.map(u => u.name),
O.getOrElse(() => 'No users')
)
vs Nullable
// Nullable: concise and safe for a short access chain
const name = user?.profile?.name ?? 'Guest'
// Option: explicit and composable across multiple operations
pipe(
O.fromNullable(user),
O.flatMap(u => O.fromNullable(u.profile)),
O.flatMap(p => O.fromNullable(p.name)),
O.getOrElse(() => 'Guest')
)
Use Option when you need to chain operations on optional values.
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.
常见问题
使用 “Option” 和直接检查值是否为 “null” 或 “undefined” 有什么区别?
在需要连续进行多重检查时,“Option” 更安全,因为它会强制你有条理地明确处理两种情况:值存在,或值不存在。
我可以把 “null” 或 “undefined” 转换成 “Option” 吗?
可以。你可以使用 `fromNullable` 这类函数,把可能为空的值转换成 “Option”。
如果我对一个不存在的值使用 “Option”,会发生什么?
通常不会报错,而是得到一个默认值,或者一个表示该值不存在的结果,具体取决于你的设置方式。
详情
作者的原始描述
Quick reference for Option type. Use when user needs to handle nullable values, optional data, or wants to avoid null checks.
热度
- GitHub 星标
- 48k
- 排名
- #4643
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
00-andruia-consultant
48k帮你的智能体启动或改进 AI 项目。它可以分析代码、推荐工具,并用西班牙语制定行动计划。
007
48k让你的智能体执行安全审计和威胁建模。它有助于保护项目免受攻击和漏洞的威胁。
10-andruia-skill-smith
48k帮你的智能体创建或改进 AI 工具。它可以定义工具的工作方式、编写文档,并验证其结构。
20-andruia-niche-intelligence
48k让你的智能体创建某个特定行业的摘要。它有助于借助可靠的信息来源,了解项目的背景、规则、使用模式和风险。