Skip to main content

What does the symbol type do?

What the symbol type does in TypeScript

The symbol type represents a unique and immutable value, which is often used as an identifier (key) for object properties.

It was added in ES2015 (ES6) and has been supported by TypeScript from the start.


The essence of symbol

Every time you create a Symbol(), it generates a unique value, even if you pass the same description (the description is just a label for debugging).

javascript
const id1 = Symbol("user"); const id2 = Symbol("user"); console.log(id1 === id2); // false - symbols are unique!

So even with the same name "user", these are different symbols.


How to declare a variable of type symbol

1. Implicitly (TS infers the type itself):

javascript
const token = Symbol("auth"); // type: symbol

2. Explicitly specifying the type:

javascript
let key: symbol = Symbol("key");

Symbols as object keys

symbol is often used to add a unique property to an object, one that will not be accidentally overwritten or shown during iteration.

javascript
const ID = Symbol("id"); const user = { name: "Tim", [ID]: 12345, }; console.log(user.name); // "Tim" console.log(user[ID]); // 12345

Properties keyed by a symbol are not visible when iterating via for...in or Object.keys():

javascript
console.log(Object.keys(user)); // ["name"]

Why symbol is needed

  1. To protect data - unique properties that cannot be accidentally overwritten.
  2. To create "hidden" keys in libraries, so they don't collide with other properties.
  3. To describe unique constants, especially in enum-like structures.
  4. For metaprogramming - used in special "well-known symbols".

"Well-known symbols"

JavaScript has a set of built-in symbols that let you change the standard behavior of objects:

SymbolPurpose
Symbol.iteratorDefines iteration of an object (for...of)
Symbol.toStringTagDefines what Object.prototype.toString() returns
Symbol.hasInstanceDefines the behavior of instanceof
Symbol.toPrimitiveControls conversion of an object to a primitive
Symbol.asyncIteratorFor asynchronous iterators
Symbol.match, Symbol.replace, Symbol.search, Symbol.splitLet you customize work with strings and RegExp

Example:

javascript
const person = { [Symbol.toPrimitive](hint) { if (hint === "number") return 42; return "Tim"; } }; console.log(+person); // 42 console.log(`${person}`); // "Tim"

Features and limitations

  1. Symbols cannot be automatically converted to a string:
javascript
const s = Symbol("id"); console.log("ID: " + s); // TypeError console.log(s.toString()); // "Symbol(id)"
  1. Symbols are unique, but you can get shared (global) ones via Symbol.for():
javascript
const a = Symbol.for("shared"); const b = Symbol.for("shared"); console.log(a === b); // true - the same global symbol

Summary

PropertyDescription
TypePrimitive
Created viaSymbol() or Symbol.for()
UniquenessAlways unique (except global ones via Symbol.for)
Main useUnique object keys, safe identifiers
Conversion to stringOnly manually via .toString()
Access to valueOnly via [symbol], not via dot notation

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.