TypeScript Concepts

Branded types — distinguishing domain values

A branded type adds a static marker to an underlying structural type, allowing otherwise similar domain values to be distinguished. A unique symbol key minimizes unintended structural overlap. The marker in this primitive intersection is erased; construct branded values through a checked boundary. An assertion can bypass the distinction and does not validate the string. Observed output: 'u-42'. Local example verified with TypeScript 5.8.3, strict checking, ES2022/CommonJS and Node.js 22.16.0; declarations enabled.

Formal Syntax & Structure

typescript syntaxtypescript
type UserId = string & { readonly [brand]: "UserId" };

Working Example

Example Snippettypescript
declare const brand: unique symbol;type UserId = string & { readonly [brand]: "UserId" };function parseId(value: string): UserId {  if (!/^u-[0-9]+$/.test(value)) throw new Error("Invalid user ID");  return value as UserId;}const id = parseId("u-42");console.log(id);
TypeScript Reference