Skip to content

Brand

Brand: object

Defined in: Types/Brand.ts:25

unwrap: <K, T>(branded) => T

Strips the brand and returns the underlying value. Since Brand<K, T> extends T this is rarely needed, but can improve readability.

K extends string

T

Brand<K, T>

T

type UserId = Brand<"UserId", string>;
const toUserId = Brand.wrap<"UserId", string>();
const userId: UserId = toUserId("user-123");
const raw: string = Brand.unwrap(userId); // "user-123"

wrap: <K, T>() => (value) => Brand<K, T>

Returns a constructor that wraps a value of type T in brand K. The resulting function performs an unchecked cast — only use when the raw value is known to satisfy the brand’s invariants.

K extends string

T

(value) => Brand<K, T>

type PositiveNumber = Brand<"PositiveNumber", number>;
const toPositiveNumber = Brand.wrap<"PositiveNumber", number>();

const n: PositiveNumber = toPositiveNumber(42);