Nominal tokens
Define private or collision-resistant capability tokens shared across modules.
TypeScript · Const
unique symbol gives each symbol a declaration-specific identity. It is useful for strongly typed tokens and symbol-keyed APIs where distinct symbols must remain incompatible.
The unique symbol type gives a symbol a declaration-specific identity rather than treating it as interchangeable with every other symbol. It is restricted to const declarations and readonly static properties; use typeof the declaration when referring to that particular symbol type.
const name: unique symbol = Symbol();const RED: unique symbol = Symbol("red");
const BLUE: unique symbol = Symbol("blue");
type Color = typeof RED | typeof BLUE;
function nameOf(color: Color): string {
return color === RED ? "red" : "blue";
}
console.log(nameOf(RED));Each declared constant creates a distinct symbol type, and typeof retrieves that declaration-specific type for the union.
Define private or collision-resistant capability tokens shared across modules.
Model symbol-keyed properties with exact, declaration-specific keys.
Create discriminants that cannot accidentally overlap with other symbols.
symboltypeofreadonly static properties