🧱 Checks if a value is a primitive.
Info
The seven primitive types are string, number, boolean, bigint, symbol, null and undefined. Everything else is an object — functions included.
Syntax
TypeScript
import { isPrimitive } from '@opentf/std'; isPrimitive(val: unknown): val is Primitive
Parameters
val: The value to check.
Returns
true if the value is a primitive, false otherwise.
Behavior
nullis a primitive, despitetypeof nullbeing'object'— a mistake old enough to be permanent, and the reason atypeofcheck alone gets this wrong.Functions are objects, so they are rejected.
A boxed primitive such as
Object(1)is an object, not a primitive.
Examples
TypeScript
isPrimitive('a') //=> true isPrimitive(1) //=> true isPrimitive(true) //=> true isPrimitive(10n) //=> true isPrimitive(Symbol('a')) //=> true isPrimitive(null) //=> true isPrimitive(undefined) //=> true // Objects isPrimitive({}) //=> false isPrimitive([]) //=> false isPrimitive(new Date()) //=> false isPrimitive(() => {}) //=> false isPrimitive(Object(1)) //=> false