TypeScript Symbol
Symbol is a primitive data type introduced in ES6 that represents a unique identifier.
In TypeScript, Symbol can be used as an object property key to ensure property uniqueness.
This is very useful in scenarios such as creating private properties and avoiding property name conflicts.
Why Symbol is needed
In JavaScript, object property names are all strings, and conflicts can sometimes occur.
Symbol provides a way to create unique identifiers; each call to Symbol() creates a new, unique value.
This is very useful in scenarios such as creating private properties, defining unique constants, and implementing iterators.
Concept explanation:Symbol is one of JavaScript's primitive data types, created via the Symbol() function. Every Symbol value is unique; even symbols with the same description are not equal.
Creating a Symbol
Use the Symbol() function to create unique Symbol values.
Example
// Create a Symbol, passing in a description string (optional)
var
sym1
=
Symbol
(
"description"
)
;
var
sym2
=
Symbol
(
"description"
)
;
// Each created Symbol is unique, even if the descriptions are the same
console.
log
(
"sym1 === sym2: "
(
sym1
===
sym2
)
)
;
console.
log
(
"sym1: "
sym1.
toString
(
)
)
;
Running result:
sym1 === sym2: false
sym1: Symbol(description)
Uniqueness:This is the most important feature of Symbol: every call to Symbol() creates a new value that is not equal to any other value.
Symbol as an Object Property
Symbol can be used as an object property key, creating a unique property name.
Example
// Create a Symbol to use as a property key
var
sym
=
Symbol
(
"key"
)
;
// Use a Symbol as an object property key
var
obj
=
{
name
:
"Alice"
,
// Normal string property
[
sym
]
:
"secret value"
// Symbol property, computed property name
}
;
// Access the normal property
console.
log
(
"Normal property: "
obj.
name
)
;
// Access the Symbol property
console.
log
(
"Symbol property: "
obj
[
sym
]
)
;
// Symbol properties do not appear in JSON
console.
log
(
"Object: "
JSON.
stringify
(
obj
)
)
;
Running result:
普通属性: Alice
Symbol 属性: secret value
对象: {"name":"Alice"}
Privacy:Symbol properties do not appear in JSON serialization and will not be traversed by for...in, so they can be used to create "private" properties.
Global Symbol Registry
Use Symbol.for() to access Symbols in the global registry; the same key returns the same Symbol.
Example
// Use the Symbol.for method to create/get a global Symbol
// If the key does not exist, a new one is created; if it already exists, the existing one is returned
var
globalSym1
=
Symbol.
for
(
"global"
)
;
var
globalSym2
=
Symbol.
for
(
"global"
)
;
// Symbols with the same key are equal
console.
log
(
"Global Symbols equal: "
(
globalSym1
===
globalSym2
)
)
;
// Get the key of a Symbol
console.
log
(
"Symbol key: "
Symbol.
keyFor
(
globalSym1
)
)
;
Running result:
全局 Symbol 相等: true
Symbol key: global
Difference:Symbol() creates a new value each time, while Symbol.for() looks up or creates one in the global registry.
Built-in Symbols
JavaScript defines several built-in Symbol values for customizing language behavior.
Example
// Symbol.iterator is used to define an object's default iterator
var
arr
=
[
1
,
2
,
3
]
;
// Get the iterator of the array
var
iterator
=
arr
[
Symbol.
iterator
]
(
)
;
// Traverse using iterators
console.
log
(
"First element: "
iterator.
next
(
)
.
value
)
;
console.
log
(
"Second element: "
iterator.
next
(
)
.
value
)
;
// Symbol.toStringTag customizes the return value of an object's toString()
var
obj
=
{
[
Symbol.
toStringTag
]
:
"MyObject"
}
;
console.
log
(
"Object type: "
obj.
toString
(
)
)
;
Running result:
第一个元素: 1
第二个元素: 2
对象类型: [object MyObject]
Important:Built-in Symbols are used to customize language behavior, such as iterators and type conversion, and form the foundation of JavaScript's advanced features.
Symbol Type Annotation
Use the Symbol type for type annotations in TypeScript.
Example
// Symbol type annotation
var
sym
:
symbol
=
Symbol
(
"key"
)
;
// The object's key type is symbol, and its value type is string
var
obj
:
{
[
key
:
symbol
]
:
string
}
=
{
}
;
// Use a Symbol as a key
obj
[
sym
]
=
"value"
;
console.
log
(
"Symbol property value: "
obj
[
sym
]
)
;
Type:Using in TypeScript
symbolThe type annotation for Symbol values.
Notes
- **Uniqueness:** The value of each Symbol() call is different
- **Non-enumerable:** Symbol properties do not appear in for...in loops
- **JSON ignored:** Symbol properties are not serialized by JSON
- **Global registration:** Symbol.for() is shared in the global registry
Application scenarios:Symbol is commonly used to create private properties on objects, define unique constants, and avoid property name collisions.
Summary
Symbol is an important primitive type in TypeScript.
- **Uniqueness:** Every created Symbol is unequal
- **Property key:** Can be used as a unique property key for objects
- **Global registration:** Symbol.for() creates/gets a global Symbol
- **Built-in Symbols:** Symbol.iterator, Symbol.toStringTag, etc.
- **Type annotation:** Use the symbol type
Note:Use Symbol when you need to create unique identifiers, avoid property name collisions, or need "private" properties.