/** * This module provides types and utility functions to create and work with branded types, * which are TypeScript types with an added type tag to prevent accidental usage of a value in the wrong context. * * The `refined` and `nominal` functions are both used to create branded types in TypeScript. * The main difference between them is that `refined` allows for validation of the data, while `nominal` does not. * * The `nominal` function is used to create a new branded type that has the same underlying type as the input, but with a different name. * This is useful when you want to distinguish between two values of the same type that have different meanings. * The `nominal` function does not perform any validation of the input data. * * On the other hand, the `refined` function is used to create a new branded type that has the same underlying type as the input, * but with a different name, and it also allows for validation of the input data. * The `refined` function takes a predicate that is used to validate the input data. * If the input data fails the validation, a `BrandErrors` is returned, which provides information about the specific validation failure. * * @since 1.0.0 */ import * as Either from "@effect/data/Either" import { identity } from "@effect/data/Function" import * as Option from "@effect/data/Option" import type { Predicate, Refinement } from "@effect/data/Predicate" import * as ReadonlyArray from "@effect/data/ReadonlyArray" import type * as Types from "@effect/data/Types" /** * @since 1.0.0 * @category symbols */ export const BrandTypeId: unique symbol = Symbol.for("@effect/data/Brand") /** * @since 1.0.0 * @category symbols */ export type BrandTypeId = typeof BrandTypeId /** * @since 1.0.0 * @category symbols */ export const RefinedConstructorsTypeId: unique symbol = Symbol.for("@effect/data/Brand/Refined") /** * @since 1.0.0 * @category symbols */ export type RefinedConstructorsTypeId = typeof RefinedConstructorsTypeId /** * A generic interface that defines a branded type. * * @since 1.0.0 * @category models */ export interface Brand { readonly [BrandTypeId]: { readonly [k in K]: K } } /** * @since 1.0.0 */ export declare namespace Brand { /** * Represents a list of refinement errors. * * @since 1.0.0 * @category models */ export interface BrandErrors extends ReadonlyArray {} /** * Represents an error that occurs when the provided value of the branded type does not pass the refinement predicate. * * @since 1.0.0 * @category models */ export interface RefinementError { readonly meta: unknown readonly message: string } /** * @since 1.0.0 * @category models */ export interface Constructor> { readonly [RefinedConstructorsTypeId]: RefinedConstructorsTypeId /** * Constructs a branded type from a value of type `A`, throwing an error if * the provided `A` is not valid. */ (args: Brand.Unbranded): A /** * Constructs a branded type from a value of type `A`, returning `Some` * if the provided `A` is valid, `None` otherwise. */ option: (args: Brand.Unbranded) => Option.Option /** * Constructs a branded type from a value of type `A`, returning `Right` * if the provided `A` is valid, `Left` otherwise. */ either: (args: Brand.Unbranded) => Either.Either /** * Attempts to refine the provided value of type `A`, returning `true` if * the provided `A` is valid, `false` otherwise. */ is: Refinement, Brand.Unbranded & A> } /** * A utility type to extract a branded type from a `Brand.Constructor`. * * @since 1.0.0 * @category models */ export type FromConstructor = A extends Brand.Constructor ? B : never /** * A utility type to extract the value type from a brand. * * @since 1.0.0 * @category models */ export type Unbranded

= P extends infer Q & Brands

? Q : P /** * A utility type to extract the brands from a branded type. * * @since 1.0.0 * @category models */ export type Brands

= P extends Brand ? Types.UnionToIntersection< { [k in keyof P[BrandTypeId]]: k extends string | symbol ? Brand : never }[keyof P[BrandTypeId]] > : never /** * A utility type that checks that all brands have the same base type. * * @since 1.0.0 * @category models */ export type EnsureCommonBase< Brands extends readonly [Brand.Constructor, ...Array>] > = { [B in keyof Brands]: Brand.Unbranded> extends Brand.Unbranded> ? Brand.Unbranded> extends Brand.Unbranded> ? Brands[B] : Brands[B] : "ERROR: All brands should have the same base type" } } /** * @category alias * @since 1.0.0 */ export type Branded = A & Brand /** * Returns a `BrandErrors` that contains a single `RefinementError`. * * @since 1.0.0 * @category constructors */ export const error: (message: string, meta?: unknown) => Brand.BrandErrors = ( message: string, meta?: unknown ): Brand.BrandErrors => [{ message, meta }] /** * Takes a variable number of `BrandErrors` and returns a single `BrandErrors` that contains all refinement errors. * * @since 1.0.0 * @category constructors */ export const errors: (...errors: Array) => Brand.BrandErrors = ( ...errors: Array ): Brand.BrandErrors => ReadonlyArray.flatten(errors) /** * Returns a `Brand.Constructor` that can construct a branded type from an unbranded value using the provided `refinement` * predicate as validation of the input data. * * If you don't want to perform any validation but only distinguish between two values of the same type but with different meanings, * see {@link nominal}. * * @param refinement - The refinement predicate to apply to the unbranded value. * @param onFailure - Takes the unbranded value that did not pass the `refinement` predicate and returns a `BrandErrors`. * * @example * import * as Brand from "@effect/data/Brand" * * type Int = number & Brand.Brand<"Int"> * * const Int = Brand.refined( * (n) => Number.isInteger(n), * (n) => Brand.error(`Expected ${n} to be an integer`) * ) * * assert.strictEqual(Int(1), 1) * assert.throws(() => Int(1.1)) * * @since 1.0.0 * @category constructors */ export const refined: >( refinement: Predicate>, onFailure: (a: Brand.Unbranded) => Brand.BrandErrors ) => Brand.Constructor = >( refinement: Predicate>, onFailure: (a: Brand.Unbranded) => Brand.BrandErrors ): Brand.Constructor => { const either = (args: Brand.Unbranded): Either.Either => refinement(args) ? Either.right(args as A) : Either.left(onFailure(args)) // @ts-expect-error return Object.assign((args) => Either.match(either(args), { onLeft: (e) => { throw e }, onRight: identity }), { [RefinedConstructorsTypeId]: RefinedConstructorsTypeId, option: (args: any) => Option.getRight(either(args)), either, is: (args: any): args is Brand.Unbranded & A => Either.isRight(either(args)) }) } /** * This function returns a `Brand.Constructor` that **does not apply any runtime checks**, it just returns the provided value. * It can be used to create nominal types that allow distinguishing between two values of the same type but with different meanings. * * If you also want to perform some validation, see {@link refined}. * * @example * import * as Brand from "@effect/data/Brand" * * type UserId = number & Brand.Brand<"UserId"> * * const UserId = Brand.nominal() * * assert.strictEqual(UserId(1), 1) * * @since 1.0.0 * @category constructors */ export const nominal: >() => Brand.Constructor = >(): Brand.Constructor< A > => { // @ts-expect-error return Object.assign((args) => args, { [RefinedConstructorsTypeId]: RefinedConstructorsTypeId, option: (args: any) => Option.some(args), either: (args: any) => Either.right(args), is: (_args: any): _args is Brand.Unbranded & A => true }) } /** * Combines two or more brands together to form a single branded type. * This API is useful when you want to validate that the input data passes multiple brand validators. * * @example * import * as Brand from "@effect/data/Brand" * * type Int = number & Brand.Brand<"Int"> * const Int = Brand.refined( * (n) => Number.isInteger(n), * (n) => Brand.error(`Expected ${n} to be an integer`) * ) * type Positive = number & Brand.Brand<"Positive"> * const Positive = Brand.refined( * (n) => n > 0, * (n) => Brand.error(`Expected ${n} to be positive`) * ) * * const PositiveInt = Brand.all(Int, Positive) * * assert.strictEqual(PositiveInt(1), 1) * assert.throws(() => PositiveInt(1.1)) * * @since 1.0.0 * @category combining */ export const all: , ...Array>]>( ...brands: Brand.EnsureCommonBase ) => Brand.Constructor< Types.UnionToIntersection<{ [B in keyof Brands]: Brand.FromConstructor }[number]> extends infer X extends Brand ? X : Brand > = < Brands extends readonly [Brand.Constructor, ...Array>] >(...brands: Brand.EnsureCommonBase): Brand.Constructor< Types.UnionToIntersection< { [B in keyof Brands]: Brand.FromConstructor }[number] > extends infer X extends Brand ? X : Brand > => { const either = (args: any): Either.Either => { let result: Either.Either = Either.right(args) for (const brand of brands) { const nextResult = brand.either(args) if (Either.isLeft(result) && Either.isLeft(nextResult)) { result = Either.left([...result.left, ...nextResult.left]) } else { result = Either.isLeft(result) ? result : nextResult } } return result } // @ts-expect-error return Object.assign((args) => Either.match(either(args), { onLeft: (e) => { throw e }, onRight: identity }), { [RefinedConstructorsTypeId]: RefinedConstructorsTypeId, option: (args: any) => Option.getRight(either(args)), either, is: (args: any): args is any => Either.isRight(either(args)) }) }