Aller au contenu principal

isSymbol()

isSymbol(value): value is symbol

Checks if a value is a symbol.


Parameters

value: unknown

The value to check.


Returns: value is symbol

true if the value is a symbol, false otherwise.


Since

1.0.0


Example

isSymbol(Symbol('id'));     // => true
isSymbol(Symbol.iterator); // => true
isSymbol('symbol'); // => false
isSymbol({ sym: Symbol() }); // => false

How it works?

Type guard that checks if a value is a symbol.

Type Narrowing

Common Checks

ValueResult
Symbol() true
Symbol('desc') true
Symbol.for('key') true
Symbol.iterator true
'Symbol()' false
{ [Symbol()]: 1 } false (object)

Use Cases

Check for symbols 📌

Verify if a value is a Symbol. Useful for checking object property keys that are meant to be private/unique.

if (isSymbol(key)) {
// Handle symbol key
}