Sub topic 10
Composing and Refining Brands
Prerequisites
Assume you know TypeScript type aliases, generics, intersection types, and the difference between compile-time types and runtime values. This chapter uses the Brand<T, B> helper from the previous chapter.
A brand can represent a fact that has already been checked. For example, Email can mean “a string with valid email syntax.” A second brand can represent a stronger fact, such as “this email address has been verified.”
Building a Refinement
Use an object of boolean keys for the brand tag:
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">;Email has the Email marker. VerifiedEmail intersects Email with another marker, so it has both Email and Verified.
The relationship is directional:
declare function logEmail(value: Email): void;
declare function sendNewsletter(value: VerifiedEmail): void;
declare const email: Email;
declare const verified: VerifiedEmail;
logEmail(verified); // OK: verified is also an Email
sendNewsletter(verified); // OK
sendNewsletter(email); // Error: Email lacks the Verified markerA refined type can be used wherever its less specific type is expected. The reverse is unsafe because an ordinary Email might not have been verified.
Refining Through a Function
A refinement function should validate the additional fact at runtime and return the stronger type. The cast belongs inside this controlled function, after the check has passed.
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">;
function verifyEmail(email: Email, code: string): VerifiedEmail {
if (code !== "123456") {
throw new Error("Verification failed");
}
return email as VerifiedEmail;
}
function sendNewsletter(to: VerifiedEmail): void {
console.log(`Sending newsletter to ${to}`);
}The function's return type records the result of the check. Callers do not need to repeat the cast or understand how the verification works.
Composing More Than Two Brands
You can add several refinements by nesting Brand:
type OwnedEmail = Brand<Email, "Owned">;
type VerifiedOwnedEmail = Brand<OwnedEmail, "Verified">;
declare const value: VerifiedOwnedEmail;
const email: Email = value; // OK
const owned: OwnedEmail = value; // OK
const verified: VerifiedEmail = value; // OKBecause the brand property contains keys, the intersections accumulate. A type with more keys satisfies a type requiring fewer keys. This is more useful than a single literal property, where independently created brands can be difficult to compose.
Common Error
Do not create a refinement merely by renaming a type:
type PretendVerifiedEmail = Email;This adds no information. It is exactly the same type as Email. A refinement must add a distinct brand and should be produced only after the corresponding runtime check.
Exercise
If VerifiedEmail is defined as Brand<Email, "Verified">, can a VerifiedEmail be passed to a function expecting Email? Can an Email be passed to a function expecting VerifiedEmail?
Show answer
The first assignment is valid because VerifiedEmail contains the Email brand. The second is rejected because an Email does not contain the Verified brand.