Skip to content

Branded Types

A branded type attaches a compile-time tag to a primitive value without altering its runtime representation. Use Brand when two values share the same underlying type (such as two strings or numbers) but carry distinct domain meaning, constructing brands at the boundary where values enter or are validated.

APIPurposeOn invalid input
Brand.nominal<T>()Unchecked constructor for already-trusted valuesNever fails
Brand.make<T>(check)Validated constructor with a synchronous predicateThrows Brand.BrandError
Brand.all(a, b, ...)Combines multiple brand checks on the same base typeThrows Brand.BrandError
Ctor.option(x)Safe validation returning Option.Option<T>Returns None
Ctor.result(x)Safe validation returning Result.Result<T, BrandError>Returns Result.fail
Ctor.is(x)Type guard predicateReturns false

Distinguishing identical representations

type InvoiceId = string & Brand.Brand<"InvoiceId">
type CustomerId = string & Brand.Brand<"CustomerId">
 
function invoiceUrl(id: InvoiceId) {
  return `/invoices/${id}`
}
 
function customerUrl(id: CustomerId) {
  return `/customers/${id}`
}
// Run in an effect function: try Console.log(invoiceUrl("inv-3" as InvoiceId), customerUrl("customer-5" as CustomerId))
/invoices/inv-3 /customers/customer-5

Plain string aliases are interchangeable at compile time, whereas InvoiceId and CustomerId are incompatible types so customerUrl rejects an InvoiceId. Branding adds no runtime wrapper or fields and performs no validation on its own.

Nominal brands

type InvoiceId = string & Brand.Brand<"InvoiceId">
const InvoiceId = Brand.nominal<InvoiceId>()
 
function knownInvoice(raw: string) {
  const id = InvoiceId(raw)
  return { id, url: `/invoices/${id}` }
}
// Run in an effect function: try Console.log(try Effect.try(() => JSON.stringify(knownInvoice("inv-3"))))
{"id":"inv-3","url":"/invoices/inv-3"}

Brand.nominal creates an identity constructor whose return type carries the brand. Because it performs no runtime checks, restrict it to values whose provenance is already known.

Refined brands and validation

type WholeCents = number & Brand.Brand<"WholeCents">
const WholeCents = Brand.make<WholeCents>((n) =>
  Number.isSafeInteger(n) || "amount must be a safe integer"
)
 
error InvalidAmount {
  cause: unknown
}
 
effect function parseAmount(raw: number) {
  return fromResult(WholeCents.result(raw)) catch effect (cause) => { throw new InvalidAmount({ cause }) }
}
// WholeCents!InvalidAmount
// Run in an effect function:
//   try Console.log(try parseAmount(125))
//   try Console.log(parseAmount(1.5) catch (error) => error._tag)
125
InvalidAmount

Brand.make takes a synchronous predicate that returns true or a failure message. Calling the constructor directly throws Brand.BrandError on invalid input; in effect code, prefer fromResult(WholeCents.result(raw)) (mapping the error with catch when needed) or wrap the call in Effect.try.

function inspectAmount(raw: number) {
  const optional = WholeCents.option(raw)
  const result = WholeCents.result(raw)
  const valid = WholeCents.is(raw)
  return { optional, result, valid }
}
// Run in an effect function:
//   const valid = inspectAmount(125)
//   const invalid = inspectAmount(1.5)
//   try Console.log(Option.isSome(valid.optional), Result.isSuccess(valid.result), valid.valid)
//   try Console.log(Option.isNone(invalid.optional), Result.isFailure(invalid.result), invalid.valid)
true true true
true true false

option, result, and is validate without throwing, returning Option, Result (with BrandError on failure), or a type-guard boolean. To surface BrandError directly in the error channel E, run try fromResult(WholeCents.result(raw)); uncaught exceptions remain defects.

Composing constraints

type WholeCents = number & Brand.Brand<"WholeCents">
const WholeCents = Brand.make<WholeCents>((n) =>
  Number.isSafeInteger(n) || "amount must be a safe integer"
)
 
type Positive = number & Brand.Brand<"Positive">
const Positive = Brand.make<Positive>((n) => n > 0 || "amount must be positive")
 
const Charge = Brand.all(WholeCents, Positive)
type Charge = Brand.Brand.FromConstructor<typeof Charge>
 
error InvalidCharge {
  cause: unknown
}
 
effect function chargeAmount(raw: number) {
  return fromResult(Charge.result(raw)) catch effect (cause) => { throw new InvalidCharge({ cause }) }
}
// chargeAmount(125) succeeds; -5 and 1.5 fail with InvalidCharge.
// Run in an effect function:
//   try Console.log(try chargeAmount(125))
//   for (const input of [-5, 1.5]) { try Console.log(chargeAmount(input) catch (error) => error._tag) }
125
InvalidCharge
InvalidCharge

Brand.all intersects multiple brand constructors over the same base type, enforcing every check and attaching every constituent brand. Derive the type alias with Brand.Brand.FromConstructor<typeof Charge> so the type stays synchronized with its constraints.

Coming from Effect

Branded types retain their standard Effect types and runtime behavior. For typed failures in VibeLang, lift validation with fromResult(Ctor.result(raw)) or Effect.try, then execute with try or recover with catch.


The matching Effect page: effect.website/docs/v4/code-style/branded-types