# gvergnaud/ts-pattern

**Attribution required: if you use, quote, or summarise this content, you must credit and link back to [awesome-repositories.com](https://awesome-repositories.com/repository/gvergnaud-ts-pattern).**

15,034 stars · 168 forks · TypeScript · MIT

## Links

- GitHub: https://github.com/gvergnaud/ts-pattern
- awesome-repositories: https://awesome-repositories.com/repository/gvergnaud-ts-pattern.md

## Topics

`branching` `conditions` `exhaustive` `inference` `javascript` `matching` `pattern` `pattern-matching` `ts` `type-inference` `typescript`

## Description

This library provides a declarative approach to conditional logic in TypeScript, replacing imperative branching with structural pattern matching. It functions as a tool for decomposing complex data structures and mapping them to specific execution branches, ensuring that all possible states are accounted for during development.

The library distinguishes itself by enforcing exhaustive logic, which requires developers to address every possible branch of a data structure to prevent runtime errors. By leveraging compile-time type narrowing and recursive evaluation, it allows for the validation and transformation of deeply nested objects while maintaining strict type safety throughout the control flow.

Beyond its core matching capabilities, the library supports complex data validation and functional state transformation. It provides a framework for evaluating incoming data against defined structural requirements, ensuring that variations in input are processed in a predictable and type-safe manner.

## Tags

### Programming Languages & Runtimes

- [Pattern Matching](https://awesome-repositories.com/f/programming-languages-runtimes/language-features-paradigms/language-features/control-flow/pattern-matching.md) — Provides a type-safe utility for exhaustive pattern matching and data structure decomposition in TypeScript.
- [Type Narrowing](https://awesome-repositories.com/f/programming-languages-runtimes/language-features-paradigms/type-system-tools/type-definitions/variable-type-declarations/type-narrowing.md) — Refines variable types within conditional branches based on structural pattern matching.

### Software Engineering & Architecture

- [Pattern Matching Libraries](https://awesome-repositories.com/f/software-engineering-architecture/pattern-matching-libraries.md) — Maps input data to specific execution branches using a declarative syntax that replaces traditional imperative conditional logic blocks.
- [Exhaustiveness Checkers](https://awesome-repositories.com/f/software-engineering-architecture/error-handling/exception-logic-structures/exception-hierarchies/exception-type-enforcers/exhaustiveness-checkers.md) — Require every possible branch of a data structure to be addressed during development to prevent unexpected runtime errors when processing incomplete or missing state information. ([source](https://github.com/gvergnaud/ts-pattern/tree/main/docs/))
- [Type-Level Conditional Logic](https://awesome-repositories.com/f/software-engineering-architecture/type-level-conditional-logic.md) — Implements robust branching logic that guarantees type safety and exhaustive coverage at compile time.
- [Recursive Validation Engines](https://awesome-repositories.com/f/software-engineering-architecture/recursive-validation-engines.md) — Handles nested data structures by dynamically dispatching validation logic across type hierarchies.
- [Type-Safe Data Handling](https://awesome-repositories.com/f/software-engineering-architecture/type-safe-data-handling.md) — Ensures all possible data states are covered at compile time through type-safe control flow mechanisms.
- [Functional State Management](https://awesome-repositories.com/f/software-engineering-architecture/architectural-design-patterns/state-management/state-logic-and-utilities/functional-state-management.md) — Processes complex nested objects by mapping patterns to functional logic for reliable state transformation.

### Data & Databases

- [Data Validation](https://awesome-repositories.com/f/data-databases/data-governance-modeling/data-management-governance/data-integrity-validation/data-validation.md) — Enforces structural rules and type requirements on incoming data to ensure safe processing.
- [Structural Data Validators](https://awesome-repositories.com/f/data-databases/structural-data-validators.md) — Evaluate incoming data against specific structural requirements and execute the corresponding logic to ensure every variation of the input is processed safely and predictably throughout the application. ([source](https://github.com/gvergnaud/ts-pattern/tree/main/docs/))
