Unique symbol
Concept and syntax
unique symbol represents one particular symbol identity, rather than the general symbol type. Declare a const symbol and use typeof in a type position to refer to its specific type. Two independently created const symbols have distinct types. This helps identify keys or tokens that must not be accidentally interchanged. At runtime they are ordinary JavaScript symbols; the static restriction is not a security mechanism.
Working example
Compile standalone examples in a fresh directory with tsc --strict --target ES2022 --module commonjs --noEmitOnError main.ts, then node main.js.
const ticket: unique symbol = Symbol("ticket");const copy: typeof ticket = ticket;const other: symbol = Symbol("ticket");console.log(copy === ticket, other === ticket);Expected output (one line per log):
true falseCommon mistake
Intentionally invalid; check this snippet separately. Do not run it.
const first: unique symbol = Symbol();const second: unique symbol = Symbol();const copy: typeof first = second;Expected compiler diagnostic: TS2322. The two const symbols have different identities even if both are symbols. Use the original symbol or choose the broader symbol type intentionally.
Practice and recap
Create two const symbols and a variable typed as typeof the first. Try assigning each symbol independently.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Unique symbol — knowledge check
Check the concept, apply it to a new choice, and interpret a compiler diagnostic.
What does typeof ticket mean in a type position here?
Which binding can directly have the unique symbol annotation?
Why is second not assignable to typeof first?
const first: unique symbol = Symbol();const second: unique symbol = Symbol();const copy: typeof first = second;