or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-jest-get-type

A utility function to get the type of a value

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/jest-get-type@29.6.x

To install, run

npx @tessl/cli install tessl/npm-jest-get-type@29.6.0

index.mddocs/

jest-get-type

A 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.

Package Information

  • Package Name: jest-get-type
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install jest-get-type

Core Imports

import { getType, isPrimitive } from "jest-get-type";

For CommonJS:

const { getType, isPrimitive } = require("jest-get-type");

Basic Usage

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({}));       // false

Capabilities

Type Detection

Get 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:

  • Correctly identifying arrays as "array" instead of "object"
  • Properly handling null as "null" instead of "object"
  • Detecting specific object subtypes: RegExp, Map, Set, Date
  • Comprehensive coverage of all JavaScript primitive and built-in object types

Usage 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"

Primitive Type Checking

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:

  • null
  • undefined
  • boolean
  • number (including NaN and Infinity)
  • string
  • symbol
  • bigint

Usage 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());    // false

Return Types

The 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.

Error Handling

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.