Chuyển đến nội dung
Skillpedia
Lập trình chung48k · #4643

fp-option-ref

Skill này làm gì

Công cụ này giúp bạn xử lý các giá trị có thể có hoặc không có trong mã. Nó cung cấp cách an toàn để làm việc với dữ liệu có thể thiếu, ngăn ngừa các lỗi thường gặp.

Khi nào nên dùng

  • ✓Khi bạn cần kiểm tra một giá trị có tồn tại không trước khi dùng.
  • ✓Khi làm việc với dữ liệu có thể đến từ nguồn bên ngoài và không đầy đủ.
  • ✓Khi muốn tránh thông báo lỗi khi không tìm thấy giá trị mong đợi.

Liên kết skill

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/fp-option-ref/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/fp-option-ref/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Làm sao để kiểm tra an toàn xem tên người dùng có tồn tại không?”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Nó dùng một khái niệm gọi là 'Option', có thể là 'Some(value)' nếu giá trị tồn tại, hoặc 'None' nếu không. Điều này cho phép nối các thao tác một cách an toàn.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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.

Câu hỏi thường gặp

Khác biệt giữa dùng 'Option' và chỉ kiểm tra xem một giá trị có phải 'null' hoặc 'undefined' không?

'Option' an toàn hơn cho các chuỗi kiểm tra dài, vì nó buộc bạn phải xử lý rõ ràng và có tổ chức cả hai trường hợp (có giá trị hoặc không có giá trị).

Tôi có thể chuyển giá trị 'null' hoặc 'undefined' thành 'Option' không?

Có, bạn có thể dùng các hàm như `fromNullable` để biến một giá trị có thể là null thành 'Option'.

Điều gì xảy ra nếu tôi dùng một giá trị không tồn tại với 'Option'?

Thay vì báo lỗi, bạn thường nhận được một giá trị mặc định hoặc kết quả cho biết giá trị không có mặt, tùy theo cách bạn cấu hình.

Chi tiết

Mô tả gốc của tác giả

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

Độ phổ biến

Sao GitHub
48k
Vị trí xếp hạng
#4643
Giấy phép
MIT
Cập nhật
15/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự