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 typeNameand the runtime constructor/guard valueName.export brand Name = …exports both. - Contextual keywords.
brandis recognized only at declaration start ([export] [global] brand Name = …) on the same line asName, soconst brand = …stays ordinary JavaScript.whereis recognized only inside abranddeclaration 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 declaringbrand Id = stringnever unify by accident. Abranddeclared 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 = ParentwhereParentis itself abrand,Sub(x)acceptsParent— never the completely unbranded underlying type — so a caller cannot bypassParent's refinement. IfSubalso declares awherepredicate, that predicate receivesParentand returnsSub!Brand.BrandError. - Nominal vs refined call contract:
- Nominal (
brand Name = Base, nowhereand not intersecting a refined brand):Name(x)is pure, infallible, zero-cost identity returningName. - Refined (
wherepresent, or intersecting any refined brand):Name(x)returnsName!Brand.BrandError(an Effect, executed withtry Name(x)), keeping VibeLang free of untyped throws.Name.is(x),Name.option(x),Name.result(x),Name.unsafe(x)(throwing escape hatch), andName.brand(raw EffectBrand.Constructor) are always attached.
- Nominal (
x is Namenarrowing:- Parses as a relational expression with no line terminator before
is(matching VIP-3's same-line rule sox\nis(y)remains two statements with zero ASI ambiguity). - Lowers to
Name.is(x)and narrowsxtoNamein TypeScript control-flow analysis (if,while,&&, ternaries,.filter,matchguards). - Applying
x is Nameto a nominal brand (no runtime predicate) is a compile error (EK8301): there is no predicate to test at runtime; construct explicitly withName(x).
- Parses as a relational expression with no line terminator before
- Program-global brands (
global brandandvibe.globalBrands).- A
branddeclared withglobal brand Name = …(or re-exported from a module listed in"vibe": { "globalBrands": ["./src/brands.vibe", …] }intsconfig.json/vibe.config.json, matching VIP-30) is in scope across every.vibemodule 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. Twoglobal branddeclarations claiming the same bare name in one program reportEK8305.
- A
- Integration with
schema(VIP-15). Referencing abrandin aschemafield (schema Transfer { to: Address; amount: Uint256 }) lowers throughSchema.fromBrand(Name.brand)composed over the base schema when the base is schema-mappable. - Pure predicates. A
wherepredicate must be synchronous and pure:try,await, andthrowinsidewhere (...) => …are compile errors (EK8304). - Declaration output. Emitted
.d.tsand.d.vibe.tsfiles emit standard TypeScripttype Name = Brand.Branded<…>anddeclare const Name: …declarations, so.tsconsumers see identical branded types with no VibeLang-specific declaration extensions.
Static rules
| Mistake | Code |
|---|---|
x is Name where Name is a nominal brand (no runtime predicate) or not a brand | EK8301 |
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 throw | EK8304 |
Two global brand declarations in one program claim the same bare name | EK8305 |
Composed brand A & B where A and B do not share a common base type | EK8306 |
Relationship to other proposals
- Schema
brand/where(VIP-77 onalpha/integrate): VIP-77 adds inlinebrandandwhereclauses insideschemadeclarations for JSON/Schema codecs. VIP-83 provides standalonebranddeclarations that compile directly toeffect/Brandwith zeroSchemaoverhead, support non-JSON primitives (Uint8Array,bigint), addx is Namenarrowing, and allow program-global brands (such as domain atoms for languages built on VibeLang). Both emit standardBrand.Brandtypes 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 / Flowopaque type, Kotlinvalue class— lightweight nominal wrappers. - Swift / Rust / SQL
where— refinement predicates. - Kotlin / C# / Dart
x is Tand 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
brandcalls (e.g., provingUint8(6n)statically valid withouttry, connecting to compile-time evaluation). - Generic brands (
brand NonEmpty<T> = readonly T[] where (xs) => xs.length > 0).