Sub topic 3
Flavored Types: Brands Without the Cast
Assume you know type aliases, intersections, generics, and the difference between compile-time types and runtime values.
Flavored types
A strict brand usually requires a constructor or a cast before a plain value becomes branded. A flavored type is a weaker alternative. It keeps plain values assignable while still preventing different flavors from being mixed.
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; // No cast needed
// @ts-expect-error CustomerId and ProductId have different flavors
const productId: ProductId = customerId;The optional property allows a string to flow into CustomerId. Because the property has different literal types, CustomerId and ProductId remain incompatible. This improves safety without requiring validation at every call site. It does not prove that "c_1" is a real customer ID.
Composing and refining brands
A brand can build on another brand. Store brand names as object keys so intersections accumulate cleanly.
declare const brands: unique symbol;
type Brand<T, B extends string> = T & {
readonly [brands]: { readonly [K in B]: true };
};
type Email = Brand<string, "Email">;
type VerifiedEmail = Brand<Email, "Verified">;
declare const verified: VerifiedEmail;
declare const email: Email;
function logEmail(value: Email): void {
console.log(value);
}
logEmail(verified); // A verified email is also an email
// sendNewsletter(email) would be rejected if it required VerifiedEmail.What brands cannot protect you from
Brands disappear when TypeScript emits JavaScript. They do not validate JSON, database rows, or user input. A type assertion can also bypass every guarantee:
const unsafe = "not-an-email" as Email;Keep assertions inside validation functions, rather than spreading them through application code.
Branding with zod
Zod combines runtime validation with a compile-time brand.
import { z } from "zod";
const UserId = z.string().min(1).brand<"UserId">();
type UserId = z.infer<typeof UserId>;
const id: UserId = UserId.parse("u_123");parse throws for invalid input and returns the branded type for valid input.
Alternatives and when to brand
Wrapper objects and classes provide runtime identity and behavior, but they add allocation, serialization work, and API complexity. Use them when the value needs methods or runtime checks. Use brands when the value can remain a primitive and the main risk is mixing domain meanings.
Brand strict types for important boundaries and units. Use flavored types when migration cost matters and validation is handled elsewhere. Avoid both when the distinction adds more ceremony than safety.
Exercise
Should a function accepting a VerifiedEmail also accept an ordinary Email?
Show answer
No. Verification is a refinement, so only values that carry both the Email and Verified brands should be accepted.