- Home
- Skills
- Martinffx
- Claude Code Atelier
- Functional Patterns
functional-patterns_skill
4
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill martinffx/claude-code-atelier --skill functional-patterns- SKILL.md12.5 KB
Overview
This skill provides practical functional programming patterns for building reliable, type-safe TypeScript systems. It focuses on ADTs, discriminated unions, Option/Result types, branded types, and small helpers that make illegal states unrepresentable. Use it to encode business rules in the type system and catch errors at compile time.
How this skill works
It supplies paste-ready types and utilities (Option, Result, Brand) plus patterns for exhaustive pattern matching and smart constructors. The helpers narrow types early, enable safe composition of fallible operations, and enforce invariants at construction time. The approach relies on consistent discriminant fields and assertNever for exhaustiveness checks so the compiler surfaces missing cases.
When to use it
- Modeling state machines (transaction lifecycle, workflows) where states are mutually exclusive
- Replacing nullable values and null/undefined checks with explicit Option types
- Encoding recoverable failure paths with Result instead of exceptions
- Preventing unit confusion and enforcing validated primitives with branded types
- Refactoring boolean flags or ad-hoc state into safer discriminated unions
Best practices
- Use a single discriminant field (_tag, kind or type) consistently across unions
- Include assertNever in default switch branches to get compiler exhaustiveness errors
- Validate in smart constructors and return branded types to enforce invariants
- Prefer Result for expected failures and Option for expected absence; use exceptions for programmer errors
- Enable strict TypeScript flags (strictNullChecks, noImplicitReturns, strictFunctionTypes) before adopting
Example use cases
- Transaction state machine: pending → settled → failed → reversed with compile-time checks
- Configuration parsing: return Result<Config, ConfigError> with field-level errors
- User lookup: return Option<User> to make absence explicit and safe to compose
- Financial logic: use Cents branded type and smart constructors to avoid floating point/unit bugs
- Domain IDs and value objects: UserId, OrderId as branded strings to prevent accidental mixing
FAQ
Option and Result make absence and recoverable errors explicit in types, forcing callers to handle those cases and avoiding hidden runtime failures.
When should I use branded types?
Use branded types for validated primitives (currency, timestamps, IDs) when you need nominal distinctions or strong invariants enforced at construction.