Type guard that checks if a value is a primitive type.
This function identifies JavaScript primitive types, which are immutable data
types that are not objects. The primitive types are: string, number,
boolean, undefined, symbol, bigint, and null.
Important Note: Although null has typeof null === "object" due to a
historical JavaScript quirk, this function correctly identifies null as a
primitive value.
Type Narrowing Behavior:
Narrows the input type to Primitive (union of all primitive types)
Excludes object types, arrays, functions, and other non-primitive values
Includes null despite its misleading typeof result
Parameters
u: unknown
The value to check
Returns uisPrimitive
true if u is a primitive type, false otherwise. When true,
TypeScript narrows the type to Primitive.
Type guard that checks if a value is a primitive type.
This function identifies JavaScript primitive types, which are immutable data types that are not objects. The primitive types are:
string,number,boolean,undefined,symbol,bigint, andnull.Important Note: Although
nullhastypeof null === "object"due to a historical JavaScript quirk, this function correctly identifiesnullas a primitive value.Type Narrowing Behavior:
Primitive(union of all primitive types)nulldespite its misleadingtypeofresult