Huge news!Announcing our $20M Series A led by Andreessen Horowitz.Learn more →
Log inDemoInstall


Package Overview
File Explorer

Advanced tools

Install Socket

Protect your apps from supply chain attacks



Type check values


Version published
Weekly downloads
decreased by-0.92%

Weekly downloads

Package description

What is @sindresorhus/is?

The @sindresorhus/is npm package is a type-checking library that provides a variety of functions to assert the type of a given value. It can be used to check primitive types, built-in objects, and to perform environment checks.

What are @sindresorhus/is's main functionalities?

Primitive type checking

Check if a value is a string.

const is = require('@sindresorhus/is');
console.log(is.string('Hello World')); // true

Built-in object checking

Check if a value is a Promise.

const is = require('@sindresorhus/is');
console.log(is.promise(new Promise(resolve => resolve()))); // true

Environment checking

Check if the current environment is Node.js.

const is = require('@sindresorhus/is');
console.log(is.nodejs); // true or false depending on the environment

Array checking

Check if a value is an Array.

const is = require('@sindresorhus/is');
console.log(is.array([1, 2, 3])); // true

Object checking

Check if a value is an Object.

const is = require('@sindresorhus/is');
console.log(is.object({foo: 'bar'})); // true

Other packages similar to @sindresorhus/is




Type check values

For example, is.string('🦄') //=> true


  • Written in TypeScript
  • Extensive use of type guards
  • Supports type assertions
  • Aware of generic type parameters (use with caution)
  • Actively maintained
  • Millions of downloads per week


npm install @sindresorhus/is


import is from '@sindresorhus/is';

//=> 'string'

is(new Map());
//=> 'Map'

//=> true

Assertions perform the same type checks, but throw an error if the type does not match.

import {assert} from '@sindresorhus/is';

//=> Error: Expected value which is `string`, received value of type `number`.

And with TypeScript:

import {assert} from '@sindresorhus/is';

// `foo` is now typed as a `string`.

Named exports

Named exports allow tooling to perform tree-shaking, potentially reducing bundle size by including only code from the methods that are used.

Every method listed below is available as a named export. Each method is prefixed by either is or assert depending on usage.

For example:

import {assertNull, isUndefined} from '@sindresorhus/is';



Returns the type of value.

Primitives are lowercase and object types are camelcase.


  • 'undefined'
  • 'null'
  • 'string'
  • 'symbol'
  • 'Array'
  • 'Function'
  • 'Object'

This method is also exported as detect. You can import it like this:

import {detect} from '@sindresorhus/is';

Note: It will throw an error if you try to feed it object-wrapped primitives, as that's a bad practice. For example new String('foo').


All the below methods accept a value and return a boolean for whether the value is of the desired type.


Note: is.number(NaN) returns false. This intentionally deviates from typeof behavior to increase user-friendliness of is type checks.

Built-in types
.array(value, assertion?)

Returns true if value is an array and all of its items match the assertion (if provided).

is.array(value); // Validate `value` is an array.
is.array(value, is.number); // Validate `value` is an array and all of its items are numbers.

Keep in mind that functions are objects too.


Returns true for a string that represents a number satisfying is.number, for example, '42' and '-8.3'.

Note: 'NaN' returns false, but 'Infinity' and '-Infinity' return true.


Returns true for any object with a .then() and .catch() method. Prefer this one over .nativePromise() as you usually want to allow userland promise implementations too.


Returns true for any object that implements its own .next() and .throw() methods and has a function definition for Symbol.iterator.


Returns true for any async function that can be called with the await operator.

is.asyncFunction(async () => {});
//=> true

is.asyncFunction(() => {});
//=> false
	(async function * () {
		yield 4;
//=> true

	(function * () {
		yield 4;
//=> false
is.asyncGeneratorFunction(async function * () {
	yield 4;
//=> true

is.asyncGeneratorFunction(function * () {
	yield 4;
//=> false

Returns true for any bound function.

is.boundFunction(() => {});
//=> true

is.boundFunction(function () {}.bind(null));
//=> true

is.boundFunction(function () {});
//=> false
Typed arrays
Structured data
.enumCase(value, enum)

TypeScript-only. Returns true if value is a member of enum.

enum Direction {
	Ascending = 'ascending',
	Descending = 'descending'

is.enumCase('ascending', Direction);
//=> true

is.enumCase('other', Direction);
//=> false

Returns true if the value is a string and the .length is 0.


Returns true if is.emptyString(value) or if it's a string that is all whitespace.


Returns true if the value is a string and the .length is more than 0.


Returns true if the value is a string that is not empty and not whitespace.

const values = ['property1', '', null, 'property2', '    ', undefined];

//=> ['property1', 'property2']

Returns true if the value is an Array and the .length is 0.


Returns true if the value is an Array and the .length is more than 0.


Returns true if the value is an Object and Object.keys(value).length is 0.

Please note that Object.keys returns only own enumerable properties. Hence something like this can happen:

const object1 = {};

Object.defineProperty(object1, 'property1', {
	value: 42,
	writable: true,
	enumerable: false,
	configurable: true

//=> true

Returns true if the value is an Object and Object.keys(value).length is more than 0.


Returns true if the value is a Set and the .size is 0.


Returns true if the value is a Set and the .size is more than 0.


Returns true if the value is a Map and the .size is 0.


Returns true if the value is a Map and the .size is more than 0.

.directInstanceOf(value, class)

Returns true if value is a direct instance of class.

is.directInstanceOf(new Error(), Error);
//=> true

class UnicornError extends Error {}

is.directInstanceOf(new UnicornError(), Error);
//=> false

Returns true if value is an instance of the URL class.

const url = new URL('');

//=> true

Returns true if value is a URL string.

Note: this only does basic checking using the URL class constructor.

const url = '';

//=> true

is.urlString(new URL(url));
//=> false

Returns true for all values that evaluate to true in a boolean context:

//=> true

//=> false

Returns true if value is one of: false, 0, '', null, undefined, NaN.


JavaScript primitives are as follows:

  • null
  • undefined
  • string
  • number
  • boolean
  • symbol
  • bigint

Returns true if value is a safe integer.


An object is plain if it's created by either {}, new Object(), or Object.create(null).


Returns true if the value is a class constructor.


A value is array-like if it is not a function and has a value.length that is a safe integer greater than or equal to 0.

//=> true

function foo() {
	//=> true
.tupleLike(value, guards)

A value is tuple-like if it matches the provided guards array both in .length and in types.

is.tupleLike([1], [is.number]);
//=> true
function foo() {
	const tuple = [1, '2', true];
	if (is.tupleLike(tuple, [is.number, is.string, is.boolean])) {
		tuple // [number, string, boolean]


Check if value is a number and is more than 0.


Check if value is a number and is less than 0.

.inRange(value, range)

Check if value (number) is in the given range. The range is an array of two values, lower bound and upper bound, in no specific order.

is.inRange(3, [0, 5]);
is.inRange(3, [5, 0]);
is.inRange(0, [-2, 2]);
.inRange(value, upperBound)

Check if value (number) is in the range of 0 to upperBound.

is.inRange(3, 10);

Returns true if value is an HTMLElement.


Returns true if value is a Node.js stream.

import fs from 'node:fs';

//=> true

Returns true if value is an Observable.

import {Observable} from 'rxjs';

is.observable(new Observable());
//=> true

Check if value is Infinity or -Infinity.


Returns true if value is an even integer.


Returns true if value is an odd integer.


Returns true if value can be used as an object property key (either string, number, or symbol).


Returns true if value is an instance of the FormData class.

const data = new FormData();

//=> true

Returns true if value is an instance of the URLSearchParams class.

const searchParams = new URLSearchParams();

//=> true
.any(predicate | predicate[], ...values)

Using a single predicate argument, returns true if any of the input values returns true in the predicate:

is.any(is.string, {}, true, '🦄');
//=> true

is.any(is.boolean, 'unicorns', [], new Map());
//=> false

Using an array of predicate[], returns true if any of the input values returns true for any of the predicates provided in an array:

is.any([is.string, is.number], {}, true, '🦄');
//=> true

is.any([is.boolean, is.number], 'unicorns', [], new Map());
//=> false
.all(predicate, ...values)

Returns true if all of the input values returns true in the predicate:

is.all(is.object, {}, new Map(), new Set());
//=> true

is.all(is.string, '🦄', [], 'unicorns');
//=> false

Returns true if the value is a safe integer that is greater than or equal to zero.

This can be useful to confirm that a value is a valid count of something, ie. 0 or more.


Returns true if the value is a string with only whitespace characters.

Type guards

When using is together with TypeScript, type guards are being used extensively to infer the correct type inside if-else statements.

import is from '@sindresorhus/is';

const padLeft = (value: string, padding: string | number) => {
	if (is.number(padding)) {
		// `padding` is typed as `number`
		return Array(padding + 1).join(' ') + value;

	if (is.string(padding)) {
		// `padding` is typed as `string`
		return padding + value;

	throw new TypeError(`Expected 'padding' to be of type 'string' or 'number', got '${is(padding)}'.`);

padLeft('🦄', 3);
//=> '   🦄'

padLeft('🦄', '🌈');
//=> '🌈🦄'

Type assertions

The type guards are also available as type assertions, which throw an error for unexpected types. It is a convenient one-line version of the often repetitive "if-not-expected-type-throw" pattern.

import {assert} from '@sindresorhus/is';

const handleMovieRatingApiResponse = (response: unknown) => {
	// `response` is now typed as a plain `object` with `unknown` properties.

	// `response.rating` is now typed as a `number`.

	// `response.title` is now typed as a `string`.

	return `${response.title} (${response.rating * 10})`;

handleMovieRatingApiResponse({rating: 0.87, title: 'The Matrix'});
//=> 'The Matrix (8.7)'

// This throws an error.
handleMovieRatingApiResponse({rating: '🦄'});

Generic type parameters

The type guards and type assertions are aware of generic type parameters, such as Promise<T> and Map<Key, Value>. The default is unknown for most cases, since is cannot check them at runtime. If the generic type is known at compile-time, either implicitly (inferred) or explicitly (provided), is propagates the type so it can be used later.

Use generic type parameters with caution. They are only checked by the TypeScript compiler, and not checked by is at runtime. This can lead to unexpected behavior, where the generic type is assumed at compile-time, but actually is something completely different at runtime. It is best to use unknown (default) and type-check the value of the generic type parameter at runtime with is or assert.

import {assert} from '@sindresorhus/is';

async function badNumberAssumption(input: unknown) {
	// Bad assumption about the generic type parameter fools the compile-time type system.
	// `input` is a `Promise` but only assumed to be `Promise<number>`.

	const resolved = await input;
	// `resolved` is typed as `number` but was not actually checked at runtime.

	// Multiplication will return NaN if the input promise did not actually contain a number.
	return 2 * resolved;

async function goodNumberAssertion(input: unknown) {
	// `input` is typed as `Promise<unknown>`

	const resolved = await input;
	// `resolved` is typed as `unknown`

	// `resolved` is typed as `number`

	// Uses runtime checks so only numbers will reach the multiplication.
	return 2 * resolved;

badNumberAssumption(Promise.resolve('An unexpected string'));
//=> NaN

// This correctly throws an error because of the unexpected string value.
goodNumberAssertion(Promise.resolve('An unexpected string'));


Why yet another type checking module?

There are hundreds of type checking modules on npm, unfortunately, I couldn't find any that fit my needs:

  • Includes both type methods and ability to get the type
  • Types of primitives returned as lowercase and object types as camelcase
  • Covers all built-ins
  • Unsurprising behavior
  • Well-maintained
  • Comprehensive test suite

For the ones I found, pick 3 of these.

The most common mistakes I noticed in these modules was using instanceof for type checking, forgetting that functions are objects, and omitting symbol as a primitive.

Why not just use instanceof instead of this package?

instanceof does not work correctly for all types and it does not work across realms. Examples of realms are iframes, windows, web workers, and the vm module in Node.js.

For enterprise

Available as part of the Tidelift Subscription.

The maintainers of @sindresorhus/is and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.




Last updated on 26 Oct 2023

Did you know?

Socket installs a GitHub app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.

SocketSocket SOC 2 Logo


  • Package Issues
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc