functional-typescript
Installation
SKILL.md
Functional TypeScript
Comprehensive guide to writing idiomatic functional TypeScript and JavaScript. Contains 20 rules across 7 categories, grounded in You Don't Know JS Yet by Kyle Simpson, extended with TypeScript type system patterns.
When to Apply
Reference these guidelines when:
- Writing new TypeScript or JavaScript functions
- Reviewing code for mutation, side effects, or imperative patterns
- Designing module APIs or encapsulated state
- Implementing data transformation pipelines
- Handling errors and nullable values without exceptions or null checks
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Core Principles | CRITICAL | core- |
| 2 | Immutability | CRITICAL | immut- |
| 3 | Closures & Scope | HIGH | closure- |
| 4 | Function Patterns | HIGH | fn- |
| 5 | Module Pattern | MEDIUM-HIGH | module- |
| 6 | TypeScript FP Types | MEDIUM | types- |
| 7 | Code Style | LOW-MEDIUM | style- |
| 8 | fp-ts Library | HIGH (when fp-ts present) | fp-ts- |
Quick Reference
1. Core Principles (CRITICAL)
core-pure-functions— return same output for same inputs; no side effectscore-side-effect-isolation— functional core, imperative shellcore-referential-transparency— injectDate.now(),Math.random(), and other hidden inputs
2. Immutability (CRITICAL)
immut-avoid-mutation— never mutate arguments; use spread and mapimmut-readonly-types— annotate params withReadonly<T>andreadonly T[]immut-const-assertion— useas constfor literal types; prefer overenumimmut-spread-over-push— usetoSorted,toReversed, spread instead ofsort,push,splice
3. Closures & Scope (HIGH)
closure-encapsulate-state— closures for private state without classesclosure-loop-pitfall— uselet(notvar) in loops; prefer array methodsclosure-memoize— memoize pure functions with closure-private cache
4. Function Patterns (HIGH)
fn-partial-application— pre-fill arguments; return specialised functionsfn-currying— one argument at a time; put config first, data lastfn-composition-pipe— replace nested calls withpipefn-higher-order—map/filter/reduceover imperative loopsfn-point-free— pass function references directly; eliminate wrapper lambdas
5. Module Pattern (MEDIUM-HIGH)
module-iife-singleton— IIFE for single-instance modules with private statemodule-factory— factory function for multiple independent instancesmodule-esm-private— export functions not raw state; unexported = private
6. TypeScript FP Types (MEDIUM)
types-result—Result<T, E>instead of throwing for expected errorstypes-option—Option<T>to make absence explicittypes-discriminated-union— discriminated unions with exhaustiveness checkingtypes-type-guard— reusablex is Tpredicates; use withfiltertypes-hof-generics— type HOFs with generics;Transform<A,B>,Predicate<T>
7. Code Style (LOW-MEDIUM)
style-prefer-functions-over-classes— factory functions avoidthisbinding bugsstyle-naming-conventions— verbs for functions,is/hasfor predicates,make/createfor factories
8. fp-ts Library (HIGH — when fp-ts is present)
Apply when the codebase imports from fp-ts/*.
fp-ts-pipe-flow—pipefor immediate transforms;flowfor reusable functionsfp-ts-option-either—Option<T>for absence;Either<E, A>for typed errors; accumulate all errors withgetApplicativeValidationfp-ts-taskeither—TaskEither<E, A>for async;tryCatch,chain,traverseArray,orElse,foldfp-ts-do-notation—Do + bind + apS + bindTofor multi-step workflows;bindwhen step depends on prior values,apSwhen independent
How to Use
Read individual rule files for detailed explanations and before/after examples:
rules/core-pure-functions.md
rules/immut-avoid-mutation.md
rules/fn-composition-pipe.md
rules/fp-ts-pipe-flow.md
rules/fp-ts-option-either.md
rules/fp-ts-taskeither.md
rules/fp-ts-do-notation.md
Each rule file contains:
- Impact level and description
- Incorrect code example with explanation
- Correct code example with explanation
- Real-world patterns and edge cases
- Reference link
References
references/pragmatic-fp.md— 80/20 guide: when to use FP, when to skip it, common refactorsreferences/fp-typescript-types.md— TypeScript type system tools for FPreferences/fp-ts-library.md— fp-ts API cheat sheet (pipe/flow, Option, Either, TaskEither, Do, RTE)references/closures-and-partial-application.md— closures, currying, memoizationreferences/module-pattern.md— IIFE, factory, ESM encapsulation
Full Compiled Guide
For all rules expanded in one document: AGENTS.md