pure-function-pattern

Enforce pure-function patterns with explicit inputs and discriminated error unions in TypeScript.

Updated Mar 9, 2026
One-click install
npx skills add https://github.com/whchi/agentic-coding --skill pure-function-pattern
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pure-function-pattern
Source: https://github.com/whchi/agentic-coding/tree/main/skills/pure-function-pattern
Command: npx skills add https://github.com/whchi/agentic-coding --skill pure-function-pattern

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This pattern helps you implement or refactor core business logic so it produces consistent outputs without side effects, making code easier to test and reason about.

Core Features & Use Cases

  • Pure input-output behavior: Functions rely only on provided inputs and return deterministic results.
  • Explicit purity contracts: Helpers return a stable discriminated union to express success or specific errors.
  • Testability and reuse: Unit tests cover all branches; logic is extractable and easy to compose.

Quick Start

Expose a small module with a main pure function and helpers, inject runtime data as explicit parameters, and cover all branches with tests.

Frequently Asked Questions about pure-function-pattern

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write deterministic business logic in TypeScript that is easy to unit test?

To write deterministic business logic in TypeScript, enforce pure-function patterns where functions rely only on provided inputs and return consistent outputs without side effects. Inject runtime data as explicit parameters and colocate unit tests to cover all branches.

What is the best way to handle errors in pure functions without throwing exceptions?

Handling errors in pure functions requires returning a stable discriminated union that explicitly expresses success or specific error states. This explicit purity contract ensures your TypeScript modules remain side-effect-free and fully testable across all logic branches.

How do I refactor existing TypeScript code to remove hidden side effects?

To refactor TypeScript code and remove hidden side effects, extract core logic into pure modules with explicit input-output behavior. Inject runtime data as parameters instead of accessing external state, ensuring functions produce deterministic results for validation and transformation tasks.

Does pure function pattern work for validation and data transformation tasks?

Yes, the pure function pattern works for validation and data transformation tasks by enforcing explicit input-output behavior. Functions process injected runtime data deterministically, returning well-defined error unions for failures, making the logic highly testable and reusable.

Why should I use discriminated unions for error handling in functional TypeScript?

You should use discriminated unions for error handling in functional TypeScript because they provide an explicit purity contract for success and failure states. This approach avoids side effects from thrown exceptions, ensuring deterministic outputs that are easy to unit test.

When should I not use pure functions for my core logic?

You should not use pure functions when tasks inherently require immediate side effects, such as direct database writes or network calls. Pure functions are designed for deterministic validation, transformation, and calculation, where runtime data must be injected as explicit parameters.