Aller au contenu principal

isPrimitive()

isPrimitive(value): value is string | number | bigint | boolean | symbol | null | undefined

Checks if a value is a primitive (string, number, boolean, null, undefined, symbol, bigint).


Parameters

value: unknown

The value to check.


Returns: value is string | number | bigint | boolean | symbol | null | undefined

true if the value is a primitive, false otherwise.


Since

2.0.0


Example

isPrimitive(1);              // => true
isPrimitive('a'); // => true
isPrimitive(true); // => true
isPrimitive(null); // => true
isPrimitive(undefined); // => true
isPrimitive({}); // => false
isPrimitive([]); // => false
isPrimitive(new Date()); // => false

How it works?

Type guard that checks if a value is a primitive type.

Primitive Types

Common Checks

ValueResult
'hello' true
123 true
true true
null true
undefined true
Symbol() true
123n true
{} false
[] false
() => {} false

Use Cases

Check for simple values 📌

Verify if a value is a string, number, boolean, null, undefined, symbol, or bigint. Essential for deep cloning recursion or serialization logic.

if (isPrimitive(value)) {
return value; // Return as-is
}