Skip to content

Brand type ​

A nominal type: a T that cannot be mixed up with another T of a different brand.

ts
import type { Brand } from 'utils';

Signature ​

ts
export type Brand<T, TBrand extends string> = T & { readonly [brand]: TBrand };

Parameters ​

NameDescription
TThe underlying type.
TBrandThe brand name.

Example ​

ts
type UserId = Brand<string, 'UserId'>;
function toUserId(value: string): UserId {
  return value as UserId;
}

Tests ​

Line coverage: 0 %.

Source ​

src/types/brand.ts (on GitHub)

Source code
ts
declare const brand: unique symbol;

/**
 * A nominal type: a `T` that cannot be mixed up with another `T` of a different brand.
 *
 * @template T - The underlying type.
 * @template TBrand - The brand name.
 * @example
 * type UserId = Brand<string, 'UserId'>;
 * function toUserId(value: string): UserId {
 *   return value as UserId;
 * }
 */
export type Brand<T, TBrand extends string> = T & { readonly [brand]: TBrand };

Keywords: brand, types, type

Generated from the JSDoc of the sources.