{"id":11104,"library":"is-type-of","title":"is-type-of: Complete Type Checking for Node.js","description":"is-type-of is a comprehensive utility library for performing various type checks on JavaScript values, primarily targeting Node.js environments. It offers a wide array of functions to identify primitive types (like string, number, boolean, bigint, symbol, null, undefined), standard JavaScript objects (array, function, object), and Node.js-specific constructs such as generator functions and async functions. The package ships with full TypeScript support, including type guards, enhancing development with strong typing. The current stable version is 2.2.0, released in December 2024, which includes Node.js 14 support for `hasOwn`. The library maintains a regular release cadence, with version 2.1.0 (November 2023) introducing dual CommonJS and ES Module support using `tshy`, ensuring broad compatibility. Its key differentiators include its extensive and specific type checking methods and seamless TypeScript integration, making it a reliable tool for robust type validation.","status":"active","version":"2.2.0","language":"javascript","source_language":"en","source_url":"git://github.com/node-modules/is-type-of","tags":["javascript","typeof","checker","type","is","typescript"],"install":[{"cmd":"npm install is-type-of","lang":"bash","label":"npm"},{"cmd":"yarn add is-type-of","lang":"bash","label":"yarn"},{"cmd":"pnpm add is-type-of","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary `is` object is exported as a default. Use default import for the main utility object.","wrong":"import { is } from 'is-type-of';","symbol":"is","correct":"import is from 'is-type-of';"},{"note":"Individual type checking functions (e.g., `isArray`, `isString`) are named exports. Do not use default import for these.","wrong":"import isArray from 'is-type-of';","symbol":"isArray","correct":"import { isArray } from 'is-type-of';"},{"note":"Since v2.1.0, the package supports both ESM and CommonJS. For CommonJS, `require('is-type-of')` returns the main `is` object, which contains all type-checking methods as properties (e.g., `is.isArray`). Direct destructuring of named exports via `require` is not the intended pattern; use `is.isArray` instead.","wrong":"const { isArray } = require('is-type-of');","symbol":"is","correct":"const is = require('is-type-of');"},{"note":"For TypeScript users, type definitions are included. Specific types for type guards are inferred or can be imported if needed for advanced scenarios, though direct type imports like `BigInt` are less common for a utility focused on runtime checks.","symbol":"BigInt","correct":"import type { BigInt } from 'is-type-of';"}],"quickstart":{"code":"import is, { isArray, isString } from 'is-type-of';\n\nconsole.log('--- Using default import (is.method) ---');\nconsole.log('Is [] an array?', is.array([]));\nconsole.log('Is 123 a number?', is.number(123));\nconsole.log('Is \"hello\" a string?', is.string('hello'));\nconsole.log('Is {} an object?', is.object({}));\nconsole.log('Is function*() {} a generator function?', is.generatorFunction(function* () {}));\n\nconsole.log('\\n--- Using named imports (isMethod) ---');\nconsole.log('Is [] an array (named)?', isArray([]));\nconsole.log('Is \"world\" a string (named)?', isString('world'));\n\nfunction processData(data: string[] | string) {\n  // TypeScript type guard example\n  if (isArray(data)) {\n    console.log(`\\nProcessing array of length: ${data.length}`);\n    data.forEach(item => console.log(`  - Array item: ${item}`));\n  } else {\n    console.log(`\\nProcessing string: ${data}`);\n  }\n}\n\nprocessData(['apple', 'banana']);\nprocessData('orange');\n\n// Example with a less common type check\nconst bigIntValue = BigInt(9007199254740991);\nconsole.log(`\\nIs ${bigIntValue} a BigInt?`, is.bigInt(bigIntValue));","lang":"typescript","description":"Demonstrates both default and named imports for various type checks, including basic primitives, objects, generator functions, and BigInt. It also illustrates the usage of TypeScript type guards provided by the library."},"warnings":[{"fix":"Manually iterate through array elements and apply `is.typeOf()` checks or use a third-party deep validation library if element-level validation is required.","message":"The `is.array()` and `isArray()` functions only check if the value itself is an array, not the types of its elements. Developers expecting deep array type validation will need additional logic.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always use primitive literals for values expected to be primitive types. If object wrappers might be encountered, convert them to primitives (`String(value)`) or check for both (`is.string(val) || is.object(val) && val instanceof String`).","message":"The library explicitly differentiates between primitive types (e.g., `string`, `number`, `boolean`) and their corresponding object wrappers (`String`, `Number`, `Boolean`). Functions like `is.string()` will return `false` for `new String('value')`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For ESM, use `import is from 'is-type-of'` for the main object and `import { isArray } from 'is-type-of'` for individual helpers. For CJS, use `const is = require('is-type-of')` and access methods via `is.isArray`. Ensure `\"esModuleInterop\": true` in `tsconfig.json` for TypeScript projects consuming CJS packages that export a default.","message":"While `is-type-of` supports both ES Modules (ESM) and CommonJS (CJS) since v2.1.0, incorrect import syntax can still lead to runtime errors, particularly when mixing module styles or when TypeScript's `esModuleInterop` is not configured correctly. The package's CommonJS export structure is primarily a default export containing all methods.","severity":"gotcha","affected_versions":">=2.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"If importing the main `is` object, access the method as `is.isArray(value)`. If you want `isArray` as a direct function, use named import: `import { isArray } from 'is-type-of';`.","cause":"Attempting to access a named export like `isArray` on the default imported `is` object, often due to incorrect import syntax when using ESM or TypeScript's default transpilation.","error":"TypeError: is_type_of_1.isArray is not a function"},{"fix":"Ensure `import is from 'is-type-of';` (for ESM/TypeScript) or `const is = require('is-type-of');` (for CommonJS) is used to correctly import the main utility object.","cause":"The default `is` object was not correctly imported, resulting in `is` being `undefined` or not having the expected properties.","error":"TypeError: Cannot read properties of undefined (reading 'array') (or similar for other methods)"},{"fix":"Since v2.1.0, `is-type-of` is built with `tshy` for dual ESM/CJS support. Ensure your Node.js version is recent enough to handle dual packages correctly (Node.js 12+ usually suffices, but Node.js 14+ is recommended for full feature set). If possible, refactor your consuming code to use `import` syntax if your project is ESM-first, or ensure your bundler/loader is correctly configured for dual-package consumption.","cause":"Attempting to `require()` an ES Module in a CommonJS context, or subtle module resolution issues when a project mixes ESM and CJS.","error":"ERR_REQUIRE_ESM"}],"ecosystem":"npm"}