Loading...
Loading...
Write TypeScript and JavaScript code like a top engineer using functional programming principles. Use when writing new code, reviewing existing code, or refactoring TS/JS projects. Applies pure functions, immutability, function composition, higher-order functions, declarative style, and avoiding shared state using native patterns only (no external libraries). Always analyzes the existing codebase first to understand patterns and conventions before making changes or suggestions.
npx skill4agent add adibfirman/dotfiles js-ts-fp.ts.tsx.js.jsxpackage.json| Principle | Description |
|---|---|
| Pure functions | No side effects, same input → same output |
| Immutability | Never mutate, always return new values |
| Declarative style | Describe what, not how |
| Function composition | Build complex from simple functions |
| Higher-order functions | Functions that take/return functions |
| Avoid shared state | No globals, no mutation of external state |
| Discriminated unions | TypeScript pattern matching alternative |
// Before: imperative loop
let results = [];
for (let i = 0; i < items.length; i++) {
if (items[i].active) {
results.push(transform(items[i]));
}
}
// After: declarative
const results = items
.filter(item => item.active)
.map(transform);// Before: mutation
function addItem(cart, item) {
cart.items.push(item);
cart.total += item.price;
return cart;
}
// After: immutable
function addItem(cart, item) {
return {
...cart,
items: [...cart.items, item],
total: cart.total + item.price
};
}// Before: shared state
let counter = 0;
function increment() {
counter++;
return counter;
}
// After: pure
function increment(counter) {
return counter + 1;
}// Before: nested
function process(data) {
const validated = validate(data);
if (validated) {
const transformed = transform(validated);
return format(transformed);
}
return null;
}
// After: composed (with pipe/flow)
const process = pipe(
validate,
transform,
format
);| Anti-Pattern | Refactor To |
|---|---|
| |
| |
| Nested callbacks | Composition or async/await |
| Option/Maybe type |
| Result/Either type |
| Class with mutable state | Pure functions + data |
| Global variables | Dependency injection |
| Pattern matching or lookup tables |