Effect CommunityEC
Effect Community2y ago
4 replies
HerbCaudill

Creating Branded Sub-Types in TypeScript

question on branded types: Is this the right way to do sub-brands?

// Cuid is a branded String
const Cuid = S.String.pipe(S.brand(Symbol.for('Cuid')), S.pattern(/^c[a-z0-9]{24}$/))
type Cuid = typeof Cuid.Type

// different IDs are branded Cuids
const brandedCuid = (name: string) => Cuid.pipe(S.brand(Symbol.for(name)))

const TimeEntryId = brandedCuid('TimeEntryId')
type TimeEntryId = typeof TimeEntryId.Type

const UserId = brandedCuid('UserId')
type UserId = typeof UserId.Type
Was this page helpful?