跳到正文
Skillpedia
通用编程48k · #4643

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/fp-option-ref/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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.

repo: sickn33/agentic-awesome-skills

热度

GitHub 星标
48k
排名
#4643
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能