githubEdit

Brand

The Brand module provides type-safe branding for values, allowing you to create distinct types from primitive values without runtime overhead.

Brand

Creates a branded type that wraps a value with a unique brand identifier.

import { Brand } from 'funkcia/brand';

type UserId = Brand<string, 'UserId'>;
type ProductId = Brand<string, 'ProductId'>;

const userId ='user_123' as UserId;
const productId = 'product_456' as ProductId;

declare function getUser(id: UserId): User | null;

getUser(userId); // OK
getUser(productId); // Type error: ProductId is not assignable to UserId

Brand.of

Creates a brand constructor. Can be used with or without validation.

Without validation

With validation

Creates a brand parser with validation and error handling.

Brand.unbrand

Removes the brand from a branded value, returning the underlying type.

Brand types

Brand.Unbrand

Utility type that extracts the underlying type from a branded type.

Last updated

Was this helpful?