Handling Nested Brands with TypeScript

Hi there, how do you tackle nested brands? Like this:

export type UUID = string & Brand.Brand<'uuid'>
const UuidSchema = z.string().uuid()
const UUID = Brand.refined<UUID>(
  (s) => UuidSchema.safeParse(s).success,
  (s) => Brand.error(`Invalid UUID: ${s}`)
)

type Int = number & Brand.Brand<'Int'>
const Int = Brand.refined<Int>(
  (n) => Number.isInteger(n),
  (n) => Brand.error(`Expected ${n} to be an integer`)
)

type Balance = {
  id: UUID
  amount: Int
} & Brand.Brand<'balance'>

const Balance = Brand.nominal<Balance>()


I can do Balance.either but I still need to pass id of UUID and amount Int.

I would like to do all this branding in one go, is this possible?
Was this page helpful?