Jest Get Type Utility
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
-
TypeError: (0, jest_get_type_1.getType) is not a function
cause Incorrect import statement when mixing CommonJS and ESM modules, or trying to default import a named export.fixIf using CommonJS, ensure `const { getType } = require('jest-get-type');`. If using ESM, ensure `import { getType } from 'jest-get-type';`. Do not use `import getType from 'jest-get-type';` as it is not a default export. -
Jest requires Node.js 18 or newer.
cause Running a project with Jest 30 (or `jest-get-type` v30+) on an unsupported Node.js version.fixUpgrade your Node.js environment to version 18.x or higher. Check your project's `.nvmrc` or CI/CD configuration. -
TS2307: Cannot find module 'jest-get-type' or its corresponding type declarations.
cause Missing `@types/jest` or `jest-get-type` package, or incorrect TypeScript configuration.fixEnsure `jest-get-type` and `@types/jest` (if Jest is also installed) are in your `devDependencies`. Verify `tsconfig.json` includes `node_modules/@types` in `typeRoots` and `moduleResolution` is set appropriately (e.g., `bundler`, `node16`, or `nodenext`).
Warnings
- breaking When upgrading to Jest 30 (which includes upgrading Jest's sub-packages like `jest-get-type` to v30+), Node.js versions 14, 16, 19, and 21 are no longer supported. Projects must use Node.js 18.x or newer.
- breaking Projects upgrading to Jest 30 (and thus `jest-get-type` v30+) must also update their TypeScript version to 5.4 or newer. Using older TypeScript versions with Jest 30 types will result in compilation errors.
- gotcha Upgrading to Jest 30 may necessitate regenerating all snapshots due to changes in snapshot header formats and the removal of deprecated `goo.gl` links. While not directly related to `jest-get-type`'s API, it is a common side effect for any project migrating their Jest setup.
- gotcha `jest-get-type` is primarily an internal utility of the Jest monorepo. While exported, its API and development are driven by Jest's core needs, which may lead to less independent feature development or versioning compared to standalone libraries.
Install
-
npm install jest-get-type -
yarn add jest-get-type -
pnpm add jest-get-type
Imports
- getType
const getType = require('jest-get-type');import { getType } from 'jest-get-type'; - getType
import getType from 'jest-get-type';
const { getType } = require('jest-get-type'); - GetType
import { GetType } from 'jest-get-type';import type { GetType } from 'jest-get-type';
Quickstart
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'