CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-sindresorhus--is

Type check values with comprehensive TypeScript type guards and runtime assertions

Pending
Overview
Eval results
Files

primitives.mddocs/

Primitives and Basic Types

Core type checking for JavaScript primitive values and basic type detection functionality. Provides both type detection and boolean validation for all primitive types.

Capabilities

Type Detection

The main is() function returns the type name of any value as a string. Primitives are returned as lowercase strings, object types as camelcase.

/**
 * Returns the type of value as a string
 * @param value - Any value to detect type of
 * @returns Type name string (primitives lowercase, objects camelcase)
 */
function is(value: unknown): TypeName;

// Also available as named export
function detect(value: unknown): TypeName;

type TypeName = ObjectTypeName | PrimitiveTypeName;

Usage Examples:

import is from '@sindresorhus/is';

is('hello'); // => 'string'
is(42); // => 'number'
is(true); // => 'boolean'
is(null); // => 'null'
is(undefined); // => 'undefined'
is(Symbol('test')); // => 'symbol'
is(123n); // => 'bigint'
is(new Date()); // => 'Date'
is([]); // => 'Array'
is({}); // => 'Object'

String Type Checking

Check if a value is a string primitive.

/**
 * Check if value is a string primitive
 * @param value - Value to check
 * @returns True if value is string
 */
function isString(value: unknown): value is string;

Usage Examples:

import is from '@sindresorhus/is';

is.string('hello'); // => true
is.string(123); // => false
is.string(new String('hello')); // => false (throws error for object wrappers)

// Type guard usage
function processText(input: unknown) {
  if (is.string(input)) {
    // input is now typed as string
    return input.toUpperCase();
  }
}

Number Type Checking

Check if a value is a number primitive. Note that NaN returns false to provide user-friendly behavior.

/**
 * Check if value is a number primitive (excludes NaN)
 * @param value - Value to check
 * @returns True if value is number and not NaN
 */
function isNumber(value: unknown): value is number;

Usage Examples:

import is from '@sindresorhus/is';

is.number(42); // => true
is.number(3.14); // => true
is.number(NaN); // => false (intentionally excludes NaN)
is.number('42'); // => false
is.number(Infinity); // => true
is.number(-Infinity); // => true

Boolean Type Checking

Check if a value is a boolean primitive.

/**
 * Check if value is a boolean primitive
 * @param value - Value to check
 * @returns True if value is boolean
 */
function isBoolean(value: unknown): value is boolean;

Usage Examples:

import is from '@sindresorhus/is';

is.boolean(true); // => true
is.boolean(false); // => true
is.boolean(1); // => false
is.boolean('true'); // => false

Symbol Type Checking

Check if a value is a symbol primitive.

/**
 * Check if value is a symbol primitive
 * @param value - Value to check
 * @returns True if value is symbol
 */
function isSymbol(value: unknown): value is symbol;

Usage Examples:

import is from '@sindresorhus/is';

is.symbol(Symbol('test')); // => true
is.symbol(Symbol.iterator); // => true
is.symbol('symbol'); // => false

BigInt Type Checking

Check if a value is a bigint primitive.

/**
 * Check if value is a bigint primitive
 * @param value - Value to check
 * @returns True if value is bigint
 */
function isBigint(value: unknown): value is bigint;

Usage Examples:

import is from '@sindresorhus/is';

is.bigint(123n); // => true
is.bigint(BigInt(456)); // => true
is.bigint(123); // => false
is.bigint('123n'); // => false

Null Type Checking

Check if a value is null.

/**
 * Check if value is null
 * @param value - Value to check
 * @returns True if value is null
 */
function isNull(value: unknown): value is null;

Usage Examples:

import is from '@sindresorhus/is';

is.null(null); // => true
is.null(undefined); // => false
is.null(0); // => false
is.null(''); // => false

Undefined Type Checking

Check if a value is undefined.

/**
 * Check if value is undefined
 * @param value - Value to check
 * @returns True if value is undefined
 */
function isUndefined(value: unknown): value is undefined;

Usage Examples:

import is from '@sindresorhus/is';

is.undefined(undefined); // => true
is.undefined(null); // => false

let uninitialized;
is.undefined(uninitialized); // => true

Combined Null or Undefined

Check if a value is either null or undefined.

/**
 * Check if value is null or undefined
 * @param value - Value to check
 * @returns True if value is null or undefined
 */
function isNullOrUndefined(value: unknown): value is null | undefined;

Usage Examples:

import is from '@sindresorhus/is';

is.nullOrUndefined(null); // => true
is.nullOrUndefined(undefined); // => true
is.nullOrUndefined(0); // => false
is.nullOrUndefined(''); // => false

Primitive Type Checking

Check if a value is any JavaScript primitive type.

/**
 * Check if value is a primitive type
 * @param value - Value to check
 * @returns True if value is primitive
 */
function isPrimitive(value: unknown): value is Primitive;

type Primitive = null | undefined | string | number | boolean | symbol | bigint;

Usage Examples:

import is from '@sindresorhus/is';

is.primitive('hello'); // => true
is.primitive(42); // => true
is.primitive(null); // => true
is.primitive({}); // => false
is.primitive([]); // => false
is.primitive(() => {}); // => false

NaN Detection

Check if a value is NaN (Not-a-Number).

/**
 * Check if value is NaN
 * @param value - Value to check
 * @returns True if value is NaN
 */
function isNan(value: unknown): value is number;

Usage Examples:

import is from '@sindresorhus/is';

is.nan(NaN); // => true
is.nan(Number('invalid')); // => true
is.nan(42); // => false
is.nan('NaN'); // => false

Notes

  • The library throws an error if you try to pass object-wrapped primitives (e.g., new String('foo'))
  • is.number() intentionally excludes NaN for user-friendliness, use is.nan() to check for NaN specifically
  • All primitive checks work with TypeScript type guards for compile-time type narrowing
  • Tree-shakable named exports are available: import { isString, isNumber } from '@sindresorhus/is'

Install with Tessl CLI

npx tessl i tessl/npm-sindresorhus--is

docs

assertions.md

async.md

collections.md

index.md

numbers.md

objects.md

primitives.md

strings.md

typed-arrays.md

validation.md

web-apis.md

tile.json