Brand<Keys>A generic interface that defines a branded type.
When to use
Use to define a branded type such as number & Brand<"Positive"> when
TypeScript should keep structurally identical values separate without
changing their runtime value.
export interface interface Brand<in out Keys extends string>A generic interface that defines a branded type.
When to use
Use to define a branded type such as number & Brand<"Positive"> when
TypeScript should keep structurally identical values separate without
changing their runtime value.
Namespace containing type-level helpers for working with branded types and
brand constructors.
Brand<in out function (type parameter) Keys in Brand<in out Keys extends string>Keys extends string> {
readonly [const TypeId: "~effect/Brand"TypeId]: {
readonly [function (type parameter) KK in function (type parameter) Keys in Brand<in out Keys extends string>Keys]: function (type parameter) Keys in Brand<in out Keys extends string>Keys
}
}
/**
* A constructor for a branded type that provides validation and safe
* construction methods.
*
* **When to use**
*
* Use as the shared callable interface for branded values when an API accepts
* or returns a brand constructor and callers need throwing, `Option`, `Result`,
* or type-guard validation forms.
*
* @see {@link nominal} for a constructor without runtime validation
* @see {@link make} for creating a constructor from a validation predicate
* @see {@link check} for creating a constructor from schema checks
* @see {@link all} for combining brand constructors
*
* @category models
* @since 2.0.0
*/
export interface interface Constructor<in out B extends Brand<any>>A constructor for a branded type that provides validation and safe
construction methods.
When to use
Use as the shared callable interface for branded values when an API accepts
or returns a brand constructor and callers need throwing, Option, Result,
or type-guard validation forms.
Constructor<in out function (type parameter) B in Constructor<in out B extends Brand<any>>B extends interface Brand<in out Keys extends string>A generic interface that defines a branded type.
When to use
Use to define a branded type such as number & Brand<"Positive"> when
TypeScript should keep structurally identical values separate without
changing their runtime value.
Namespace containing type-level helpers for working with branded types and
brand constructors.
Brand<any>> {
/**
* Constructs a branded type from a value of type `Unbranded<B>`, throwing an
* error if the provided value is not valid.
*/
(unbranded: Brand.Unbranded<B>unbranded: Brand.type Brand<in out Keys extends string>.Unbranded<B extends Brand<any>> = B extends infer U & Types.UnionToIntersection<{ [K in keyof B["~effect/Brand"]]: K extends string ? Brand<K> : never; }[keyof B["~effect/Brand"]]> ? U : BA utility type to extract the unbranded value type from a brand.
Unbranded<function (type parameter) B in Constructor<in out B extends Brand<any>>B>): function (type parameter) B in Constructor<in out B extends Brand<any>>B
/**
* Constructs a branded type from a value of type `Unbranded<B>`, returning
* `Some<B>` if the provided value is valid, `None` otherwise.
*/
function Constructor(unbranded: Brand.Unbranded<B>): Option.Option<B>Constructs a branded type from a value of type Unbranded<B>, returning
Some<B> if the provided value is valid, None otherwise.
option(unbranded: Brand.Unbranded<B>unbranded: Brand.type Brand<in out Keys extends string>.Unbranded<B extends Brand<any>> = B extends infer U & Types.UnionToIntersection<{ [K in keyof B["~effect/Brand"]]: K extends string ? Brand<K> : never; }[keyof B["~effect/Brand"]]> ? U : BA utility type to extract the unbranded value type from a brand.
Unbranded<function (type parameter) B in Constructor<in out B extends Brand<any>>B>): import OptionOption.type Option<A> = Option.None<A> | Option.Some<A>The Option data type represents optional values. An Option<A> is either
Some<A>, containing a value of type A, or None, representing absence.
When to use
Use to represent initial values that may not yet exist
- Returning from partial functions (not defined for all inputs)
- Managing optional fields in data structures
Namespace containing utility types for Option.
When to use
Use to access type-level helpers associated with Option.
Option<function (type parameter) B in Constructor<in out B extends Brand<any>>B>
/**
* Constructs a branded type from a value of type `Unbranded<B>`, returning
* `Success<B>` if the provided value is valid, `Failure<BrandError>`
* otherwise.
*/
function Constructor(unbranded: Brand.Unbranded<B>): Result.Result<B, BrandError>Constructs a branded type from a value of type Unbranded<B>, returning
Success<B> if the provided value is valid, Failure<BrandError>
otherwise.
result(unbranded: Brand.Unbranded<B>unbranded: Brand.type Brand<in out Keys extends string>.Unbranded<B extends Brand<any>> = B extends infer U & Types.UnionToIntersection<{ [K in keyof B["~effect/Brand"]]: K extends string ? Brand<K> : never; }[keyof B["~effect/Brand"]]> ? U : BA utility type to extract the unbranded value type from a brand.
Unbranded<function (type parameter) B in Constructor<in out B extends Brand<any>>B>): import ResultResult.type Result<A, E = never> = Result.Success<A, E> | Result.Failure<A, E>A value that is either Success<A, E> or Failure<A, E>.
When to use
Use when both success and failure should remain available as data and
Option would lose failure information.
Details
- Use
succeed
/
fail
to construct
- Use
match
to fold both branches
- Use
isSuccess
/
isFailure
to narrow the type
E defaults to never, so Result<number> means a result that cannot fail.
Example (Creating and matching a Result)
import { Result } from "effect"
const success = Result.succeed(42)
const failure = Result.fail("something went wrong")
const message = Result.match(success, {
onSuccess: (value) => `Success: ${value}`,
onFailure: (error) => `Error: ${error}`
})
console.log(message)
// Output: "Success: 42"
Namespace containing type-level utilities for extracting the inner types
of a Result.
Example (Extracting inner types)
import type { Result } from "effect"
type R = Result.Result<number, string>
// number
type A = Result.Result.Success<R>
// string
type E = Result.Result.Failure<R>
Result<function (type parameter) B in Constructor<in out B extends Brand<any>>B, class BrandErrorclass BrandError {
_tag: 'BrandError';
name: string;
issue: SchemaIssue.Issue;
message: string;
toString: () => string;
}
Error returned when a branded type is constructed from an invalid value.
Details
The error wraps a SchemaIssue.Issue, exposes message through
issue.toString(), and formats as BrandError(<message>).
Gotchas
BrandError is an error-like model with _tag, name, message, and
toString; it does not extend JavaScript Error.
BrandError>
/**
* Attempts to refine the provided value of type `Unbranded<B>`, returning
* `true` if the provided value is a valid branded type, `false` otherwise.
*/
Constructor<in out B extends Brand<any>>.is(unbranded: Brand.Unbranded<B>): unbranded is Brand.Unbranded<B> & BAttempts to refine the provided value of type Unbranded<B>, returning
true if the provided value is a valid branded type, false otherwise.
is(unbranded: Brand.Unbranded<B>unbranded: Brand.type Brand<in out Keys extends string>.Unbranded<B extends Brand<any>> = B extends infer U & Types.UnionToIntersection<{ [K in keyof B["~effect/Brand"]]: K extends string ? Brand<K> : never; }[keyof B["~effect/Brand"]]> ? U : BA utility type to extract the unbranded value type from a brand.
Unbranded<function (type parameter) B in Constructor<in out B extends Brand<any>>B>): unbranded: Brand.Unbranded<B>unbranded is Brand.type Brand<in out Keys extends string>.Unbranded<B extends Brand<any>> = B extends infer U & Types.UnionToIntersection<{ [K in keyof B["~effect/Brand"]]: K extends string ? Brand<K> : never; }[keyof B["~effect/Brand"]]> ? U : BA utility type to extract the unbranded value type from a brand.
Unbranded<function (type parameter) B in Constructor<in out B extends Brand<any>>B> & function (type parameter) B in Constructor<in out B extends Brand<any>>B
/**
* The checks that are applied to the branded type.
*
* @internal
*/
Constructor<in out B extends Brand<any>>.checks?: readonly [SchemaAST.Check<Brand.Unbranded<B>>, ...Array<SchemaAST.Check<Brand.Unbranded<B>>>] | undefinedThe checks that are applied to the branded type.
checks?: readonly [import SchemaASTSchemaAST.type Check<T> =
| SchemaAST.Filter<T>
| SchemaAST.FilterGroup<T>
Check<Brand.type Brand<in out Keys extends string>.Unbranded<B extends Brand<any>> = B extends infer U & Types.UnionToIntersection<{ [K in keyof B["~effect/Brand"]]: K extends string ? Brand<K> : never; }[keyof B["~effect/Brand"]]> ? U : BA utility type to extract the unbranded value type from a brand.
Unbranded<function (type parameter) B in Constructor<in out B extends Brand<any>>B>>, ...interface Array<T>Array<import SchemaASTSchemaAST.type Check<T> =
| SchemaAST.Filter<T>
| SchemaAST.FilterGroup<T>
Check<Brand.type Brand<in out Keys extends string>.Unbranded<B extends Brand<any>> = B extends infer U & Types.UnionToIntersection<{ [K in keyof B["~effect/Brand"]]: K extends string ? Brand<K> : never; }[keyof B["~effect/Brand"]]> ? U : BA utility type to extract the unbranded value type from a brand.
Unbranded<function (type parameter) B in Constructor<in out B extends Brand<any>>B>>>] | undefined
}
/**
* Error returned when a branded type is constructed from an invalid value.
*
* **Details**
*
* The error wraps a `SchemaIssue.Issue`, exposes `message` through
* `issue.toString()`, and formats as `BrandError(<message>)`.
*
* **Gotchas**
*
* `BrandError` is an error-like model with `_tag`, `name`, `message`, and
* `toString`; it does not extend JavaScript `Error`.
*
* @category errors
* @since 4.0.0
*/
export class class BrandErrorclass BrandError {
_tag: 'BrandError';
name: string;
issue: SchemaIssue.Issue;
message: string;
toString: () => string;
}
Error returned when a branded type is constructed from an invalid value.
Details
The error wraps a SchemaIssue.Issue, exposes message through
issue.toString(), and formats as BrandError(<message>).
Gotchas
BrandError is an error-like model with _tag, name, message, and
toString; it does not extend JavaScript Error.
BrandError {
constructor(issue: SchemaIssue.Issueissue: import SchemaIssueSchemaIssue.type Issue =
| SchemaIssue.Leaf
| SchemaIssue.Filter
| SchemaIssue.Encoding
| SchemaIssue.Pointer
| SchemaIssue.Composite
| SchemaIssue.AnyOf
The root discriminated union of all validation error nodes.
When to use
Use when typing the error channel in Effect<A, Issue, R> results from
schema parsing, or when writing custom formatters or issue-tree walkers.
Details
Every node has a _tag field for pattern-matching. The union includes both
terminal
Leaf
types and composite types that wrap inner issues:
Filter
,
Encoding
,
Pointer
,
Composite
,
AnyOf
. All Issue instances have a toString() that delegates to
the default formatter, so String(issue) produces a human-readable message.
Issue) {
this.BrandError.issue: SchemaIssue.IssueSchema issue describing why brand validation failed.
issue = issue: SchemaIssue.Issueissue
}
/**
* Discriminant used to identify brand construction failures.
*
* @since 4.0.0
*/
readonly BrandError._tag: "BrandError"Discriminant used to identify brand construction failures.
_tag = "BrandError"
/**
* Error name used by tools that inspect JavaScript error-like objects.
*
* @since 4.0.0
*/
readonly BrandError.name: stringError name used by tools that inspect JavaScript error-like objects.
name: string = "BrandError"
/**
* Schema issue describing why brand validation failed.
*
* @since 4.0.0
*/
readonly BrandError.issue: SchemaIssue.IssueSchema issue describing why brand validation failed.
issue: import SchemaIssueSchemaIssue.type Issue =
| SchemaIssue.Leaf
| SchemaIssue.Filter
| SchemaIssue.Encoding
| SchemaIssue.Pointer
| SchemaIssue.Composite
| SchemaIssue.AnyOf
The root discriminated union of all validation error nodes.
When to use
Use when typing the error channel in Effect<A, Issue, R> results from
schema parsing, or when writing custom formatters or issue-tree walkers.
Details
Every node has a _tag field for pattern-matching. The union includes both
terminal
Leaf
types and composite types that wrap inner issues:
Filter
,
Encoding
,
Pointer
,
Composite
,
AnyOf
. All Issue instances have a toString() that delegates to
the default formatter, so String(issue) produces a human-readable message.
Issue
/**
* Human-readable rendering of the validation issue.
*
* @since 4.0.0
*/
get BrandError.message: stringHuman-readable rendering of the validation issue.
message() {
return this.BrandError.issue: SchemaIssue.IssueSchema issue describing why brand validation failed.
issue.Base.toString(this: Issue): stringtoString()
}
/**
* Formats the brand error together with its validation message.
*
* @since 4.0.0
*/
BrandError.toString(): stringFormats the brand error together with its validation message.
toString() {
return `BrandError(${this.BrandError.message: stringHuman-readable rendering of the validation issue.
message})`
}
}
/**
* Namespace containing type-level helpers for working with branded types and
* brand constructors.
*
* @since 2.0.0
*/
export declare namespace Brand {
/**
* A utility type to extract a branded type from a `Constructor`.
*
* @category utility types
* @since 2.0.0
*/
export type type Brand<in out Keys extends string>.FromConstructor<C> = C extends Constructor<infer B extends Brand<any>> ? B : neverA utility type to extract a branded type from a Constructor.
FromConstructor<function (type parameter) C in type Brand<in out Keys extends string>.FromConstructor<C>C> = function (type parameter) C in type Brand<in out Keys extends string>.FromConstructor<C>C extends interface Constructor<in out B extends Brand<any>>A constructor for a branded type that provides validation and safe
construction methods.
When to use
Use as the shared callable interface for branded values when an API accepts
or returns a brand constructor and callers need throwing, Option, Result,
or type-guard validation forms.
Constructor<infer function (type parameter) BB> ? function (type parameter) BB : never
/**
* A utility type to extract the unbranded value type from a brand.
*
* @category utility types
* @since 2.0.0
*/
export type type Brand<in out Keys extends string>.Unbranded<B extends Brand<any>> = B extends infer U & Types.UnionToIntersection<{ [K in keyof B["~effect/Brand"]]: K extends string ? Brand<K> : never; }[keyof B["~effect/Brand"]]> ? U : BA utility type to extract the unbranded value type from a brand.
Unbranded<function (type parameter) B in type Brand<in out Keys extends string>.Unbranded<B extends Brand<any>>B extends interface Brand<in out Keys extends string>A generic interface that defines a branded type.
When to use
Use to define a branded type such as number & Brand<"Positive"> when
TypeScript should keep structurally identical values separate without
changing their runtime value.
Namespace containing type-level helpers for working with branded types and
brand constructors.
Brand<any>> = function (type parameter) B in type Brand<in out Keys extends string>.Unbranded<B extends Brand<any>>B extends infer function (type parameter) UU & type Brand<in out Keys extends string>.Brands<B extends Brand<any>> = ({ [K in keyof B["~effect/Brand"]]: K extends string ? Brand<K> : never; }[keyof B["~effect/Brand"]] extends any ? (x: { [K in keyof B["~effect/Brand"]]: K extends string ? Brand<K> : never; }[keyof B["~effect/Brand"]]) => any : never) extends (x: infer R) => any ? R : neverA utility type to extract the brands from a branded type.
Brands<function (type parameter) B in type Brand<in out Keys extends string>.Unbranded<B extends Brand<any>>B> ? function (type parameter) UU : function (type parameter) B in type Brand<in out Keys extends string>.Unbranded<B extends Brand<any>>B
/**
* A utility type to extract the keys of a branded type.
*
* @category utility types
* @since 4.0.0
*/
export type type Brand<in out Keys extends string>.Keys<B extends Brand<any>> = keyof B["~effect/Brand"]A utility type to extract the keys of a branded type.
Keys<function (type parameter) B in type Brand<in out Keys extends string>.Keys<B extends Brand<any>>B extends interface Brand<in out Keys extends string>A generic interface that defines a branded type.
When to use
Use to define a branded type such as number & Brand<"Positive"> when
TypeScript should keep structurally identical values separate without
changing their runtime value.
Namespace containing type-level helpers for working with branded types and
brand constructors.
Brand<any>> = keyof function (type parameter) B in type Brand<in out Keys extends string>.Keys<B extends Brand<any>>B[typeof const TypeId: "~effect/Brand"TypeId]
/**
* A utility type to extract the brands from a branded type.
*
* @category utility types
* @since 2.0.0
*/
export type type Brand<in out Keys extends string>.Brands<B extends Brand<any>> = ({ [K in keyof B["~effect/Brand"]]: K extends string ? Brand<K> : never; }[keyof B["~effect/Brand"]] extends any ? (x: { [K in keyof B["~effect/Brand"]]: K extends string ? Brand<K> : never; }[keyof B["~effect/Brand"]]) => any : never) extends (x: infer R) => any ? R : neverA utility type to extract the brands from a branded type.
Brands<function (type parameter) B in type Brand<in out Keys extends string>.Brands<B extends Brand<any>>B extends interface Brand<in out Keys extends string>A generic interface that defines a branded type.
When to use
Use to define a branded type such as number & Brand<"Positive"> when
TypeScript should keep structurally identical values separate without
changing their runtime value.
Namespace containing type-level helpers for working with branded types and
brand constructors.
Brand<any>> = import TypesTypes.type UnionToIntersection<T> = (
T extends any ? (x: T) => any : never
) extends (x: infer R) => any
? R
: never
Transforms a union type into an intersection type.
When to use
Use to combine all members of a union into a single type with all their
properties. This is useful in advanced generic code where you need to merge
union variants.
Details
- Uses distributive conditional types and contra-variant inference.
- If the union members are incompatible (e.g.
string | number), the
result is never.
Example (Converting a union to an intersection)
import type { Types } from "effect"
type Union = { a: string } | { b: number }
type Result = Types.UnionToIntersection<Union>
// { a: string } & { b: number }
UnionToIntersection<
{ [function (type parameter) KK in type Brand<in out Keys extends string>.Keys<B extends Brand<any>> = keyof B["~effect/Brand"]A utility type to extract the keys of a branded type.
Keys<function (type parameter) B in type Brand<in out Keys extends string>.Brands<B extends Brand<any>>B>]: function (type parameter) KK extends string ? interface Brand<in out Keys extends string>A generic interface that defines a branded type.
When to use
Use to define a branded type such as number & Brand<"Positive"> when
TypeScript should keep structurally identical values separate without
changing their runtime value.
Namespace containing type-level helpers for working with branded types and
brand constructors.
Brand<function (type parameter) KK> : never }[type Brand<in out Keys extends string>.Keys<B extends Brand<any>> = keyof B["~effect/Brand"]A utility type to extract the keys of a branded type.
Keys<function (type parameter) B in type Brand<in out Keys extends string>.Brands<B extends Brand<any>>B>]
>
/**
* A utility type that checks that all brands have the same base type.
*
* @category utility types
* @since 2.0.0
*/
export type type Brand<in out Keys extends string>.EnsureCommonBase<Brands extends readonly [Constructor<any>, ...Array<Constructor<any>>]> = { [B in keyof Brands]: Unbranded<FromConstructor<Brands[0]>> extends Unbranded<FromConstructor<Brands[B]>> ? Unbranded<FromConstructor<Brands[B]>> extends Unbranded<FromConstructor<Brands[0]>> ? Brands[B] : Brands[B] : "ERROR: All brands should have the same base type"; }A utility type that checks that all brands have the same base type.
EnsureCommonBase<
function (type parameter) Brands in type Brand<in out Keys extends string>.EnsureCommonBase<Brands extends readonly [Constructor<any>, ...Array<Constructor<any>>]>Brands extends readonly [interface Constructor<in out B extends Brand<any>>A constructor for a branded type that provides validation and safe
construction methods.
When to use
Use as the shared callable interface for branded values when an API accepts
or returns a brand constructor and callers need throwing, Option, Result,
or type-guard validation forms.
Constructor<any>, ...interface Array<T>Array<interface Constructor<in out B extends Brand<any>>A constructor for a branded type that provides validation and safe
construction methods.
When to use
Use as the shared callable interface for branded values when an API accepts
or returns a brand constructor and callers need throwing, Option, Result,
or type-guard validation forms.
Constructor<any>>]
> = {
[function (type parameter) BB in keyof function (type parameter) Brands in type Brand<in out Keys extends string>.EnsureCommonBase<Brands extends readonly [Constructor<any>, ...Array<Constructor<any>>]>Brands]: Brand.type Brand<in out Keys extends string>.Unbranded<B extends Brand<any>> = B extends infer U & Types.UnionToIntersection<{ [K in keyof B["~effect/Brand"]]: K extends string ? Brand<K> : never; }[keyof B["~effect/Brand"]]> ? U : BA utility type to extract the unbranded value type from a brand.
Unbranded<Brand.type Brand<in out Keys extends string>.FromConstructor<C> = C extends Constructor<infer B extends Brand<any>> ? B : neverA utility type to extract a branded type from a Constructor.
FromConstructor<function (type parameter) Brands in type Brand<in out Keys extends string>.EnsureCommonBase<Brands extends readonly [Constructor<any>, ...Array<Constructor<any>>]>Brands[0]>> extends
Brand.type Brand<in out Keys extends string>.Unbranded<B extends Brand<any>> = B extends infer U & Types.UnionToIntersection<{ [K in keyof B["~effect/Brand"]]: K extends string ? Brand<K> : never; }[keyof B["~effect/Brand"]]> ? U : BA utility type to extract the unbranded value type from a brand.
Unbranded<Brand.type Brand<in out Keys extends string>.FromConstructor<C> = C extends Constructor<infer B extends Brand<any>> ? B : neverA utility type to extract a branded type from a Constructor.
FromConstructor<function (type parameter) Brands in type Brand<in out Keys extends string>.EnsureCommonBase<Brands extends readonly [Constructor<any>, ...Array<Constructor<any>>]>Brands[function (type parameter) BB]>>
? Brand.type Brand<in out Keys extends string>.Unbranded<B extends Brand<any>> = B extends infer U & Types.UnionToIntersection<{ [K in keyof B["~effect/Brand"]]: K extends string ? Brand<K> : never; }[keyof B["~effect/Brand"]]> ? U : BA utility type to extract the unbranded value type from a brand.
Unbranded<Brand.type Brand<in out Keys extends string>.FromConstructor<C> = C extends Constructor<infer B extends Brand<any>> ? B : neverA utility type to extract a branded type from a Constructor.
FromConstructor<function (type parameter) Brands in type Brand<in out Keys extends string>.EnsureCommonBase<Brands extends readonly [Constructor<any>, ...Array<Constructor<any>>]>Brands[function (type parameter) BB]>> extends Brand.type Brand<in out Keys extends string>.Unbranded<B extends Brand<any>> = B extends infer U & Types.UnionToIntersection<{ [K in keyof B["~effect/Brand"]]: K extends string ? Brand<K> : never; }[keyof B["~effect/Brand"]]> ? U : BA utility type to extract the unbranded value type from a brand.
Unbranded<Brand.type Brand<in out Keys extends string>.FromConstructor<C> = C extends Constructor<infer B extends Brand<any>> ? B : neverA utility type to extract a branded type from a Constructor.
FromConstructor<function (type parameter) Brands in type Brand<in out Keys extends string>.EnsureCommonBase<Brands extends readonly [Constructor<any>, ...Array<Constructor<any>>]>Brands[0]>>
? function (type parameter) Brands in type Brand<in out Keys extends string>.EnsureCommonBase<Brands extends readonly [Constructor<any>, ...Array<Constructor<any>>]>Brands[function (type parameter) BB]
: function (type parameter) Brands in type Brand<in out Keys extends string>.EnsureCommonBase<Brands extends readonly [Constructor<any>, ...Array<Constructor<any>>]>Brands[function (type parameter) BB]
: "ERROR: All brands should have the same base type"
}
}