Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TypeScript · Const

unique

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.

Since
TypeScript 2.7

How it works

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.

Syntax

const name: unique symbol = Symbol();

Key points

  • unique symbol represents a symbol as a distinct literal-like type.
  • Every unique symbol type is also a symbol type, so it can be used where symbol is expected.
  • Identity comes from the declaration, allowing separate constants to act as nominally distinct keys or tokens.
  • Values can originate from Symbol or Symbol.for calls, or from an explicit unique-symbol annotation.
  • A variable with this type must be immutable through a const declaration.

Common mistakes

  • A variable explicitly annotated as unique symbol must be const; the other supported declaration form is a readonly static property.
  • Different unique symbols cannot be assigned to one another or compared as equivalent types.
  • Use typeof on the symbol declaration to refer to its exact unique-symbol type.

Example: Distinct symbol identities

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.

When to use it

Nominal tokens

Define private or collision-resistant capability tokens shared across modules.

Typed symbol keys

Model symbol-keyed properties with exact, declaration-specific keys.

Unique discriminants

Create discriminants that cannot accidentally overlap with other symbols.

  • symbol
  • typeof
  • readonly static properties

Source: Symbols. Checked 2026-10-15.