fp-ts-pragmatic
它能做什么
这份指南实用地讲解如何在 TypeScript 中使用 fp-ts 库。它聚焦于最有用的部分,帮助你写出更简洁、更易维护的代码,而不深入复杂的概念。
适用场景
- ✓刚开始接触 fp-ts,需要简单直接的示例时。
- ✓编写需要处理可能为 null 的值、错误或异步操作的 TypeScript 代码时。
- ✓想改善代码,却不想先学完整个函数式编程理论时。
技能链接
https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/fp-ts-pragmatic/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我想用简单的方式,借助 fp-ts 在 TypeScript 中处理可能为 null 的值,该怎么做?”
每周 5 个新技能,1 分钟读完。
工作原理
它通过模式和代码示例,展示 fp-ts 核心概念的应用方式。重点是直接用它们解决常见的编程问题。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Pragmatic Functional Programming
Read this first. This guide cuts through the academic jargon and shows you what actually matters. No category theory. No abstract nonsense. Just patterns that make your code better.
Detailed Guide
Read the detailed guide before executing this skill. It retains the complete procedure and reference material. Treat its safety, prerequisites, and validation requirements as mandatory. For focused work, load the relevant sections; for end-to-end work, read the guide completely.
When to Use This Skill
- When starting with fp-ts and need practical guidance
- When writing TypeScript code that handles nullable values, errors, or async operations
- When you want cleaner, more maintainable functional code without the academic overhead
- When refactoring imperative code to functional style
When NOT to Use FP
Functional programming is not always the answer. Here's when to keep it simple.
Simple Null Checks
// Just use optional chaining - it's built into the language
const city = user?.address?.city ?? 'Unknown'
// DON'T overcomplicate it
const city = pipe(
O.fromNullable(user),
O.flatMap(u => O.fromNullable(u.address)),
O.flatMap(a => O.fromNullable(a.city)),
O.getOrElse(() => 'Unknown')
)
Simple Loops
// A for loop is fine when you need early exit or complex logic
function findFirst(items: Item[], predicate: (i: Item) => boolean): Item | null {
for (const item of items) {
if (predicate(item)) return item
}
return null
}
// DON'T force FP when it doesn't help
const result = pipe(
items,
A.findFirst(predicate),
O.toNullable
)
Performance-Critical Code
// For hot paths, imperative is faster (no intermediate arrays)
function sumLarge(numbers: number[]): number {
let sum = 0
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i]
}
return sum
}
// fp-ts creates intermediate structures
const sum = pipe(numbers, A.reduce(0, (acc, n) => acc + n))
When Your Team Doesn't Know FP
If you're the only one who can read the code, it's not good code.
// If your team knows this pattern
async function getUser(id: string): Promise<User | null> {
try {
const response = await fetch(`/api/users/${id}`)
if (!response.ok) return null
return await response.json()
} catch {
return null
}
}
// Don't force this on them
const getUser = (id: string): TE.TaskEither<Error, User> =>
pipe(
TE.tryCatch(() => fetch(`/api/users/${id}`), E.toError),
TE.flatMap(r => r.ok ? TE.right(r) : TE.left(new Error('Not found'))),
TE.flatMap(r => TE.tryCatch(() => r.json(), E.toError))
)
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.
常见问题
什么是 fp-ts?
它是一个用于 TypeScript 的库,帮助你遵循函数式编程原则编写代码,让代码更安全、更有条理。
什么时候不应该使用 fp-ts?
如果只是很简单的任务,比如基本的 null 检查、简单的循环,或者对性能要求极高的代码,或者你的团队不熟悉函数式编程,就不要用它。
我需要通读 fp-ts 的完整文档吗?
不需要。这份指南聚焦于最重要的那 20%(采用 80/20 方法),让你不必经历学术化的复杂内容,也能快速见到效果。
详情
作者的原始描述
A practical, jargon-free guide to fp-ts functional programming - the 80/20 approach that gets results without the academic overhead. Use when writing TypeScript with fp-ts library.
热度
- GitHub 星标
- 48k
- 排名
- #4650
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
00-andruia-consultant
48k帮你的智能体启动或改进 AI 项目。它可以分析代码、推荐工具,并用西班牙语制定行动计划。
007
48k让你的智能体执行安全审计和威胁建模。它有助于保护项目免受攻击和漏洞的威胁。
10-andruia-skill-smith
48k帮你的智能体创建或改进 AI 工具。它可以定义工具的工作方式、编写文档,并验证其结构。
20-andruia-niche-intelligence
48k让你的智能体创建某个特定行业的摘要。它有助于借助可靠的信息来源,了解项目的背景、规则、使用模式和风险。