Sub topic 4
A reusable `Brand<T, B>` helper
Assume you know type aliases, generics, intersection types, and the difference between TypeScript's compile-time types and JavaScript's runtime values. This chapter builds a reusable helper for branded types. It does not validate data by itself.
Why use a helper?
A brand adds a type-only tag to an existing type. The tag distinguishes values that otherwise have the same shape.
Without branding, these aliases remain interchangeable:
type UserId = string;
type OrderId = string;
function loadOrder(id: OrderId): void {
console.log("loading", id);
}
const userId: UserId = "u_123";
loadOrder(userId); // AllowedThe aliases describe the same type: string. TypeScript does not preserve the domain meaning of each alias.
A reusable helper gives each type a distinct tag:
declare const brand: unique symbol;
type Brand<T, B extends string> = T & {
readonly [brand]: B;
};T is the underlying type, such as string or number. B is the brand name. B extends string requires the brand name to be a string literal or another string type.
The computed property [brand] uses a unique symbol. A unique symbol is a symbol type with a distinct identity. Code outside this module cannot accidentally create a property with the same key.
Applying the helper
Use the helper to define domain-specific types:
declare const brand: unique symbol;
type Brand<T, B extends string> = T & {
readonly [brand]: B;
};
type UserId = Brand<string, "UserId">;
type OrderId = Brand<string, "OrderId">;
function loadOrder(id: OrderId): void {
console.log("loading", id);
}
const userId = "u_123" as UserId;
const orderId = "o_456" as OrderId;
loadOrder(orderId);
loadOrder(userId); // ErrorThe intersection type means a UserId must be both a string and an object-like type containing the private brand property. A normal string does not have that property, so it is not assignable to UserId.
The brand does not change the runtime value. Both userId and orderId are ordinary strings at runtime.
| Type expression | Meaning |
|---|---|
| `Brand<string, "UserId">` | A string carrying the `UserId` compile-time tag |
| `Brand<number, "Cents">` | A number carrying the `Cents` compile-time tag |
| `Brand<UserId, "Verified">` | A `UserId` with an additional refinement |
Keep the brand key private
A module should declare its brand symbol once and reuse it:
const brand: unique symbol = Symbol("brand");
export type Brand<T, B extends string> = T & {
readonly [brand]: B;
};
export type UserId = Brand<string, "UserId">;The symbol does not need to be exported. Consumers can use UserId, but they cannot name the internal property directly. This prevents accidental collisions with application data.
readonly prevents code from assigning to the brand property. It documents that the tag is a type marker, not ordinary mutable data.
The helper is intentionally small. Validation belongs in smart constructors, type guards, or a schema library. The helper's job is to make the validated result distinct from its underlying type.
Exercise
What does Brand<OrderId, "Verified"> represent, and why is it assignable to OrderId?
Show answer
It represents an OrderId with an additional Verified tag. Because the branded type is an intersection with OrderId, it contains all of OrderId's requirements and can be used where an OrderId is expected.