A small JavaScript library for defining and using union types with associated data.
union-type is a small JavaScript library for defining and using union types, also known as algebraic data types (ADTs). It allows developers to create types that can represent multiple possible values, each potentially with associated data, enabling safer and more expressive data modeling. The library provides pattern matching via `case` and `caseOn` methods to handle different type variants concisely.
JavaScript developers working on applications that benefit from functional programming patterns, such as those building complex state management, data transformations, or domain models with multiple shapes.
Developers choose union-type for its simplicity and focus on union types without heavy dependencies, offering runtime type validation, recursive type support, and a functional API that integrates well with modern JavaScript while keeping bundle size minimal.
A small JavaScript library for defining and using union types.
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
The library exports only the `Type` function, keeping bundle size minimal and dedicated to union types without bloat, as emphasized in the README.
Provides `case` and `caseOn` methods for exhaustive and concise switching over type variants, enabling declarative code handling with examples like state management in the tutorial.
Allows definition of recursive types, such as linked lists, using `undefined` in definitions, as shown in the `List` example for modeling complex data structures.
Built-in validation throws helpful TypeError messages for constructor mismatches during development, improving debugging without external tools.
Type checking can be disabled by setting `Type.check` to `false`, allowing performance tuning in production builds without modifying code.
Designed for plain JavaScript, it lacks built-in TypeScript definitions, requiring manual type annotations or community contributions for type-safe usage.
Defining types with object-based syntax and arrays can be more verbose than modern alternatives, increasing boilerplate for complex types.
Instance methods must be attached to prototypes manually, as shown with `Maybe.prototype.map`, which can be error-prone and less intuitive than class-based approaches.
As a smaller library, it has limited tutorials, examples, and ecosystem support compared to popular solutions, potentially hindering collaboration and learning.