Sub topic 9
Flavored Types: Brands Without the Cast
Assume you know TypeScript type aliases, intersections, generics, and the difference between compile-time types and runtime values. This chapter shows a lighter alternative to strict branded types.
A flavored type is a type that keeps its original value type, such as string, while adding an optional marker. Plain values can enter the type without a cast, but values with different flavors remain incompatible.
The problem with strict brands
A strict brand usually adds a required property:
declare const brand: unique symbol;
type Brand<T, B extends string> = T & {
readonly [brand]: B;
};
type UserId = Brand<string, "UserId">;A plain string is not a UserId. You must create one through a constructor or use a type assertion:
const id = "u_123" as UserId;That strictness is useful when the value has been validated. However, it can be inconvenient when values come from trusted application code and you only want to prevent mixing different meanings.
Making the marker optional
A flavored type uses an optional property:
declare const flavor: unique symbol;
type Flavor<T, F extends string> = T & {
readonly [flavor]?: F;
};
type CustomerId = Flavor<string, "CustomerId">;
type ProductId = Flavor<string, "ProductId">;
const raw: string = "c_1";
const customerId: CustomerId = raw;
function loadCustomer(id: CustomerId): void {
console.log("loading customer", id);
}
loadCustomer(raw);
loadCustomer(customerId);
const productId: ProductId = "p_1";
// Error: CustomerId and ProductId have different flavors.
// const wrong: ProductId = customerId;The optional marker means a plain string satisfies CustomerId. The marker still gives TypeScript a structural difference between CustomerId and ProductId.
| Type relationship | Result |
|---|---|
| `string` to `CustomerId` | Allowed |
| `CustomerId` to `string` | Allowed |
| `CustomerId` to `ProductId` | Rejected |
| `ProductId` to `CustomerId` | Rejected |
The marker exists only in the type system. It does not appear in the emitted JavaScript.
When flavoring is appropriate
Flavoring works well when the main goal is to stop accidental interchange:
CustomerIdversusProductIdMetersversusSecondsDraftOrderversusSubmittedOrderInternalPathversusPublicPath
It does not prove that a value has been checked. The following assignment is accepted even if the text is invalid:
const invalid: CustomerId = "not-really-a-customer-id";Use a strict brand when every value must pass validation. Use a flavor when compatibility with existing plain values matters more than enforcing construction through a validator.
How the marker prevents mixing
The flavor is usually keyed by a unique symbol. This avoids collisions with normal object properties. Since the property is optional and readonly, it does not change the runtime representation of the value.
Choosing between strict and flavored brands
Use a strict brand when:
- the value came from untrusted input;
- an invalid value could cause a security or correctness problem;
- construction should happen only through a smart constructor;
- you want the type to document a validation guarantee.
Use a flavored type when:
- the value is already trusted;
- adding casts would create unnecessary noise;
- you mainly need to prevent mixing similar primitives;
- gradual adoption is important in an existing codebase.
Exercise
What does the optional flavor marker prevent, and what does it fail to guarantee?
Show answer
It prevents values with different flavors, such as CustomerId and ProductId, from being assigned to each other. It does not validate the underlying string or number, so an invalid plain value can still enter a flavored type.