Jest Get Type Utility

29.6.3 · active · verified Sun Apr 19

The `jest-get-type` package provides a core utility function, `getType`, designed to accurately determine the type of any JavaScript value (e.g., 'array', 'object', 'string', 'null', 'undefined', 'function', 'symbol'). It is an integral part of the Jest testing framework's monorepo, ensuring consistent type identification across Jest's various internal components. The current specific package version is 29.6.3, aligning with the Jest 29 ecosystem. However, the broader Jest framework recently released its major version 30.0.0, which targets more frequent major releases after a significant previous gap. This utility is primarily used internally by Jest, but can also be imported directly for robust, Jest-aligned type checking in other projects, especially when consistency with Jest's internal logic is desired. It ships with TypeScript types for an enhanced developer experience.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates importing `getType` and using it to determine the type of various JavaScript primitives and objects.

import { getType } from 'jest-get-type';

console.log(getType(1)); // Outputs: 'number'
console.log(getType('hello')); // Outputs: 'string'
console.log(getType(true)); // Outputs: 'boolean'
console.log(getType({})); // Outputs: 'object'
console.log(getType([])); // Outputs: 'array'
console.log(getType(null)); // Outputs: 'null'
console.log(getType(undefined)); // Outputs: 'undefined'
console.log(getType(() => {})); // Outputs: 'function'
console.log(getType(Symbol('foo'))); // Outputs: 'symbol'

interface MyObject {
  a: number;
  b: string;
}

const obj: MyObject = { a: 1, b: 'test' };
console.log(getType(obj)); // Outputs: 'object'

view raw JSON →