Symbols
A symbol
is a primitive data type that represents a globally-unique value.
You can create them with the Symbol
function, which produces a value of type symbol
.
Symbols are immutable and unique.
Just like strings, symbols can be used as keys for object properties.
Declaring unique symbols
A symbol will be treated as unique literal with the type annotation unique symbol
. A value of type unique symbol
can only be produced by calling Symbol()
or Symbol.for()
, or from explicit type annotations.
The unique symbol
type is only allowed on const
declarations and readonly static
properties. To reference a specific unique symbol, use the typeof
operator.
Keep in mind that each reference to a unique symbol
implies a completely unique identity that is tied to a given declaration. Because each unique symbol has a completely separate identity, no two unique symbol types are assignable or comparable to each other.
Well-known symbols
- Symbol.toPrimitive