Skip to content

VIP-83: brand declarations, is narrowing, and program-global brands

Status: accepted by Will, 2026-12-01.

Before its first release, VibeLang ships no quick fixes or codemods of its own. TypeScript's own code fixes remain available.

Decision

// 1. Nominal brand — zero runtime cost (Brand.nominal)
brand UserId = string
brand Wei = bigint
brand Selector = Bytes4                                           // subtype of a brand: Selector <: Bytes4
 
// 2. Refined brand — runtime predicate (Brand.make)
brand Positive = number where (n) => n > 0
brand Uint8 = bigint where (n) => n >= 0n && n <= 255n || "expected 0..255"
brand Address = Uint8Array where (b) => b.length === 20 || "expected 20 bytes"
 
// 3. Composed brand — intersection over the same base (Brand.all)
brand PositiveInt = Positive & Int
 
// 4. Program-global brand — in scope across the entire program without an import
export global brand Hash = Uint8Array where (b) => b.length === 32 || "expected 32 bytes"

Construction, widening, and narrowing

// Widening (upcasting) is implicit — a brand IS its base type
const id = UserId("u_42")                       // nominal: pure, infallible, zero runtime cost
const raw: string = id                          // ok: UserId <: string
const sel = Selector(bytes4Val)                 // ok: accepts direct parent Bytes4 (not raw Uint8Array)
const b4: Bytes4 = sel                          // ok: Selector <: Bytes4
 
// Downcasting or cross-brand assignment is a compile error (TS2322)
// const bad: UserId = "u_42"
// const bad2: Selector = rawUint8Array
 
// Control-flow narrowing with `is` (no line terminator before `is`)
if (n is Uint8) {
  // `n` is narrowed to `Uint8`
}
const valid = nums.filter((n) => n is Positive) // Positive[]
 
// Effect-safe narrowing with `try` (fails typed with BrandError, never throws)
effect function setDecimals(n: bigint): Uint8!Brand.BrandError {
  return try Uint8(n)
}
 
// Non-Effect constructors remain available
const opt = Uint8.option(n)                     // Option.Option<Uint8>
const res = Uint8.result(n)                     // Result.Result<Uint8, Brand.BrandError>

Lowering to Effect 4

Every brand declaration lowers to Effect 4's Brand module (Brand.Branded, Brand.nominal, Brand.make, Brand.all) with both type and value bound under one name (matching VIP-15 schema and VIP-16 data):

// Nominal: brand UserId = string
type UserId = Brand.Branded<string, "@acme/pkg/src/ids#UserId">
const UserId = Brand.nominal<UserId>()
 
// Subtype of a brand: brand Selector = Bytes4
type Selector = Bytes4 & Brand.Brand<"@acme/pkg/src/eth#Selector">
const Selector: {
  (value: Bytes4): Selector
  readonly brand: Brand.Constructor<Selector>
} = /* identity over Bytes4 */
 
// Refined: brand Uint8 = bigint where (n) => n >= 0n && n <= 255n || "expected 0..255"
type Uint8 = Brand.Branded<bigint, "@acme/pkg/src/eth#Uint8">
const __Uint8 = Brand.make<Uint8>((n) => n >= 0n && n <= 255n || "expected 0..255")
const Uint8 = Object.assign(
  (value: bigint) => Effect.fromResult(__Uint8.result(value)),
  {
    is: __Uint8.is,
    option: __Uint8.option,
    result: __Uint8.result,
    unsafe: __Uint8,
    brand: __Uint8,
  },
)
 
// Composed: brand PositiveInt = Positive & Int
type PositiveInt = Positive & Int
const __PositiveInt = Brand.all(Positive.brand, Int.brand)
const PositiveInt = Object.assign(
  (value: number) => Effect.fromResult(__PositiveInt.result(value)),
  {
    is: __PositiveInt.is,
    option: __PositiveInt.option,
    result: __PositiveInt.result,
    unsafe: __PositiveInt,
    brand: __PositiveInt,
  },
)
 
// Expression narrowing: x is Uint8
Uint8.is(x)

Rules

  • One name, both type and value. brand Name = … declares both the branded type Name and the runtime constructor/guard value Name. export brand Name = … exports both.
  • Contextual keywords. brand is recognized only at declaration start ([export] [global] brand Name = …) on the same line as Name, so const brand = … stays ordinary JavaScript. where is recognized only inside a brand declaration after the base type.
  • Derived unique brand keys (matches VIP-9). The nominal brand key is derived from package, file, and declaration name ("@pkg/file#Name"), so two packages declaring brand Id = string never unify by accident. A brand declared outside a package or inside a function/block without an explicit package root requires an explicit key or stays scoped to its declaration site.
  • Direct-parent construction. For brand Sub = Parent where Parent is itself a brand, Sub(x) accepts Parent — never the completely unbranded underlying type — so a caller cannot bypass Parent's refinement. If Sub also declares a where predicate, that predicate receives Parent and returns Sub!Brand.BrandError.
  • Nominal vs refined call contract:
    • Nominal (brand Name = Base, no where and not intersecting a refined brand): Name(x) is pure, infallible, zero-cost identity returning Name.
    • Refined (where present, or intersecting any refined brand): Name(x) returns Name!Brand.BrandError (an Effect, executed with try Name(x)), keeping VibeLang free of untyped throws. Name.is(x), Name.option(x), Name.result(x), Name.unsafe(x) (throwing escape hatch), and Name.brand (raw Effect Brand.Constructor) are always attached.
  • x is Name narrowing:
    • Parses as a relational expression with no line terminator before is (matching VIP-3's same-line rule so x\nis(y) remains two statements with zero ASI ambiguity).
    • Lowers to Name.is(x) and narrows x to Name in TypeScript control-flow analysis (if, while, &&, ternaries, .filter, match guards).
    • Applying x is Name to a nominal brand (no runtime predicate) is a compile error (EK8301): there is no predicate to test at runtime; construct explicitly with Name(x).
  • Program-global brands (global brand and vibe.globalBrands).
    • A brand declared with global brand Name = … (or re-exported from a module listed in "vibe": { "globalBrands": ["./src/brands.vibe", …] } in tsconfig.json / vibe.config.json, matching VIP-30) is in scope across every .vibe module in the program without an explicit import.
    • Emitted code imports each referenced global brand from its declaring module, exactly as VIP-14 imports only the Effect modules a file uses.
    • Precedence & shadowing. Local declarations and explicit imports shadow a global brand. Non-platform JS/TS globals (Array, String, Number, Proxy, Record, …) keep their bare names per VIP-14. Two global brand declarations claiming the same bare name in one program report EK8305.
  • Integration with schema (VIP-15). Referencing a brand in a schema field (schema Transfer { to: Address; amount: Uint256 }) lowers through Schema.fromBrand(Name.brand) composed over the base schema when the base is schema-mappable.
  • Pure predicates. A where predicate must be synchronous and pure: try, await, and throw inside where (...) => … are compile errors (EK8304).
  • Declaration output. Emitted .d.ts and .d.vibe.ts files emit standard TypeScript type Name = Brand.Branded<…> and declare const Name: … declarations, so .ts consumers see identical branded types with no VibeLang-specific declaration extensions.

Static rules

MistakeCode
x is Name where Name is a nominal brand (no runtime predicate) or not a brandEK8301
export default brand Name = … (declare Name and export { Name as default })EK8302
Incomplete brand declaration or generic brand Name<T> = …EK8303
where predicate contains try, await, or throwEK8304
Two global brand declarations in one program claim the same bare nameEK8305
Composed brand A & B where A and B do not share a common base typeEK8306

Relationship to other proposals

  • Schema brand/where (VIP-77 on alpha/integrate): VIP-77 adds inline brand and where clauses inside schema declarations for JSON/Schema codecs. VIP-83 provides standalone brand declarations that compile directly to effect/Brand with zero Schema overhead, support non-JSON primitives (Uint8Array, bigint), add x is Name narrowing, and allow program-global brands (such as domain atoms for languages built on VibeLang). Both emit standard Brand.Brand types and compose cleanly.

Prior art

  • Effect 4 Brand (Brand.Branded, Brand.nominal, Brand.make, Brand.all, BrandError).
  • VibeLang service (VIP-9), schema (VIP-15), data (VIP-16), and implicit modules (VIP-14).
  • Haskell newtype, Scala 3 / Flow opaque type, Kotlin value class — lightweight nominal wrappers.
  • Swift / Rust / SQL where — refinement predicates.
  • Kotlin / C# / Dart x is T and TypeScript type predicates — control-flow type-guard narrowing.

Will's words

"I think branded types will be super common. So I think we can make our language simpler by upstreaming a built in super nice way to create and use branded types in vibelang itself and then rather than being parser breaking changes in ethlang we can just use these branded types. These can be explicit like a way of declaring a type and narrowing it."

"Got it they are vibelang brands and also global to program I accept this please make this happen and make sure we push this VIP to github"

Left to later VIPs

  • Compile-time literal discharge for refined brand calls (e.g., proving Uint8(6n) statically valid without try, connecting to compile-time evaluation).
  • Generic brands (brand NonEmpty<T> = readonly T[] where (xs) => xs.length > 0).