A utility function to get the type of a value
npx @tessl/cli install tessl/npm-jest-get-type@29.6.0A utility function to get the type of a value with handling for edge cases like arrays and null values that the native typeof operator doesn't address correctly.
npm install jest-get-typeimport { getType, isPrimitive } from "jest-get-type";For CommonJS:
const { getType, isPrimitive } = require("jest-get-type");import { getType, isPrimitive } from "jest-get-type";
// Get accurate type information
console.log(getType([])); // "array" (not "object")
console.log(getType(null)); // "null" (not "object")
console.log(getType(new Date())); // "date"
console.log(getType(/regex/)); // "regexp"
console.log(getType(new Map())); // "map"
// Check if value is primitive
console.log(isPrimitive("hello")); // true
console.log(isPrimitive(42)); // true
console.log(isPrimitive(null)); // true
console.log(isPrimitive([])); // false
console.log(isPrimitive({})); // falseGet the precise type of a value, handling edge cases that native typeof doesn't address correctly.
/**
* Get the type of a value with handling for edge cases like arrays and null
* @param value - Any value to determine the type of
* @returns Precise type string
* @throws Error for values of unknown type
*/
function getType(value: unknown): ValueType;The getType function provides more accurate type detection than native typeof by:
"array" instead of "object"null as "null" instead of "object"RegExp, Map, Set, DateUsage Examples:
// Primitive types
getType(undefined); // "undefined"
getType(null); // "null"
getType(true); // "boolean"
getType(42); // "number"
getType("hello"); // "string"
getType(Symbol("id")); // "symbol"
getType(BigInt(123)); // "bigint"
// Function type
getType(() => {}); // "function"
// Object subtypes
getType([1, 2, 3]); // "array"
getType({}); // "object"
getType(/pattern/); // "regexp"
getType(new Map()); // "map"
getType(new Set()); // "set"
getType(new Date()); // "date"Determine if a value is a primitive type using the standard JavaScript definition.
/**
* Determine if a value is a primitive type
* @param value - Any value to check
* @returns true if the value is primitive, false otherwise
*/
function isPrimitive(value: unknown): boolean;Uses the standard test Object(value) !== value to determine primitiveness. Primitive types in JavaScript are:
nullundefinedbooleannumber (including NaN and Infinity)stringsymbolbigintUsage Examples:
// Primitive values
isPrimitive(null); // true
isPrimitive(undefined); // true
isPrimitive(42); // true
isPrimitive("text"); // true
isPrimitive(true); // true
isPrimitive(Symbol("x")); // true
isPrimitive(BigInt(999)); // true
isPrimitive(NaN); // true
isPrimitive(Infinity); // true
// Non-primitive values
isPrimitive({}); // false
isPrimitive([]); // false
isPrimitive(() => {}); // false
isPrimitive(/regex/); // false
isPrimitive(new Map()); // false
isPrimitive(new Set()); // false
isPrimitive(new Date()); // falseThe getType function returns one of the following string literals:
type ValueType =
| 'array'
| 'bigint'
| 'boolean'
| 'function'
| 'null'
| 'number'
| 'object'
| 'regexp'
| 'map'
| 'set'
| 'date'
| 'string'
| 'symbol'
| 'undefined';Note: ValueType is an internal type definition used for documentation purposes. It is not exported from the package and cannot be imported directly. The type information is provided here for reference to show all possible return values from the getType function.
The getType function throws an Error with the message "value of unknown type: ${value}" if it encounters a value that doesn't match any of the expected type patterns. This is a fallback case that should not occur under normal circumstances with standard JavaScript values.