Skip to main content

isSet()

isSet(value): value is Set<unknown>

Checks if a value is a Set instance.


Parametersโ€‹

value: unknownโ€‹

The value to check.


Returns: value is Set<unknown>โ€‹

true if the value is a Set, false otherwise.


See Alsoโ€‹


Sinceโ€‹

2.0.0


Exampleโ€‹

isSet(new Set());        // => true
isSet(new Set([1, 2])); // => true
isSet([]); // => false
isSet(new WeakSet()); // => false
isSet(new Map()); // => false

How it works?โ€‹

Type guard that checks if a value is a Set.

Type Narrowingโ€‹

Common Checksโ€‹

ValueResult
new Set()checkmark true
new Set([1, 2, 3])checkmark true
new WeakSet()cross false
[]cross false
{ add: () => {} }cross false

Use Casesโ€‹

Identify unique collections ๐Ÿ“Œโ€‹

Check if a value is a Set instance. Useful when working with collections that require uniqueness.

if (isSet(items)) {
console.log(items.has(value));
}