Sub topic 11
What Brands Cannot Protect You From
Assume you know TypeScript type aliases, intersections, functions, and the difference between compile-time types and runtime values. A brand prevents certain incorrect assignments while TypeScript checks your source code. It does not create a new runtime type or validate data automatically.
Brands disappear at runtime
TypeScript removes type annotations, aliases, and brands when it emits JavaScript. A branded string is still an ordinary string while the program runs.
declare const brand: unique symbol;
type Brand<T, B extends string> = T & {
readonly [brand]: B;
};
type UserId = Brand<string, "UserId">;
function loadUser(id: UserId): void {
console.log("loading", id);
}
const id = "u_123" as UserId;
console.log(typeof id); // "string"
loadUser(id);The brand declaration helps the compiler distinguish types. It does not add a property to id. You cannot use instanceof UserId, inspect the brand with Object.keys, or depend on the brand to survive serialization.
Runtime data can still be invalid
JSON, network responses, database rows, and values from JavaScript do not carry trustworthy TypeScript types. A type annotation or assertion only tells the compiler what you claim to be true.
declare const brand: unique symbol;
type Brand<T, B extends string> = T & { readonly [brand]: B };
type Email = Brand<string, "Email">;
const response = JSON.parse('{"email":"not-an-email"}');
const email = response.email as Email; // Compiles, but no validation occurred
console.log(email); // "not-an-email"The same problem occurs with any. A value of type any can flow into a branded parameter without proving anything.
Assertions can forge any brand
Type assertions bypass the checks that make a brand useful. They are sometimes necessary inside a trusted constructor, but spreading them through application code removes the protection.
declare const brand: unique symbol;
type Brand<T, B extends string> = T & { readonly [brand]: B };
type Meters = Brand<number, "Meters">;
type Seconds = Brand<number, "Seconds">;
const seconds = 30 as Seconds;
const mistaken = seconds as unknown as Meters; // Compiles, but is wrong
function reportDistance(value: Meters): void {
console.log(`${value} meters`);
}
reportDistance(mistaken); // Runs and prints a false unitBrands also do not prove that later code preserves a domain rule. Arithmetic returns a plain number, and mutable aliases can undermine assumptions if branded values are placed in poorly designed mutable structures. Revalidate values when an operation can break their invariant.
| Situation | What the brand provides | What it does not provide |
|---|---|---|
| Passing a `UserId` to `loadOrder` | Compile-time rejection | Runtime validation |
| Reading JSON as `Email` | A claimed type | Proof that the text is an email |
| Adding branded numbers | A useful input distinction | Automatic branded output |
| Casting with `as` | A way to silence an error | Evidence that the value is valid |
Keep trust at narrow boundaries
Use a smart constructor, type predicate, or assertion function near the boundary where unknown data enters. Keep the assertion inside that function, then pass the branded result through the typed part of the program.
declare const brand: unique symbol;
type Brand<T, B extends string> = T & { readonly [brand]: B };
type PositiveInt = Brand<number, "PositiveInt">;
function parsePositiveInt(value: unknown): PositiveInt {
if (
typeof value !== "number" ||
!Number.isInteger(value) ||
value <= 0
) {
throw new Error("Expected a positive integer");
}
return value as PositiveInt;
}
const pageSize = parsePositiveInt(20);
console.log(pageSize);This pattern does not make the entire program foolproof. It makes the unchecked part small, visible, and reviewable.
Exercise
Why does JSON.parse(text) as Email fail to guarantee that the result is a valid email?
Show answer
The assertion changes only the compile-time view. JSON.parse does not perform email validation, and the brand is erased at runtime.