JSPM

  • Created
  • Published
  • Downloads 154132
  • Score
    100M100P100Q188634F
  • License MIT

A set of JavaScript helper functions to check for types

Package Exports

    This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@phun-ky/typeof) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

    Readme

    @phun-ky/typeof

    Commitizen friendly PRs Welcome SemVer 2.0 npm version issues license size npm GitHub Repo stars codecov build

    About

    A set of JavaScript helper functions to check for types.

    Table of Contents

    Installation

    npm i --save @phun-ky/typeof

    Usage

    Either import and run the required functions:

    import { isString } from '@phun-ky/typeof';
    
    isString('asd'); // true;

    API

    isBoolean()

    Checks if the given value is a boolean.

    Param

    The value to check.

    Call Signature

    function isBoolean(value): value is boolean;

    Defined in: main.ts:85

    Parameters

    Parameter Type
    value unknown

    Returns

    value is boolean

    Call Signature

    function isBoolean(value): boolean;

    Defined in: main.ts:90

    Parameters

    Parameter Type
    value unknown

    Returns

    boolean


    isBuiltInCallable()

    Checks if a given value is a built-in JavaScript callable.

    A built-in callable is either:

    • a standard constructor (e.g., Object, Array, Date, Map), or
    • a callable non-constructable built-in (BigInt, Symbol).

    This function first verifies the value is a function, then tests identity against a curated set of built-ins.

    Overloads:

    • Predicate: narrows the value to BuiltInCallable on success.
    • Boolean: usable in contexts that require a plain (v) => boolean.

    Param

    The value to check.

    Example

    isBuiltInCallable(Object); // true
    isBuiltInCallable(Array); // true
    isBuiltInCallable(BigInt); // true (callable but not a constructor)
    isBuiltInCallable(Symbol); // true (callable but not a constructor)
    isBuiltInCallable(class X {}); // false
    isBuiltInCallable(() => {}); // false
    isBuiltInCallable(123); // false
    
    // Type narrowing:
    declare const fn: unknown;
    if (isBuiltInCallable(fn)) {
      // fn is now typed as BuiltInCallable
      console.log(fn.name);
    }

    See

    Call Signature

    function isBuiltInCallable(value): value is BuiltInCallable;

    Defined in: main.ts:539

    Parameters

    Parameter Type
    value unknown

    Returns

    value is BuiltInCallable

    Call Signature

    function isBuiltInCallable(value): boolean;

    Defined in: main.ts:544

    Parameters

    Parameter Type
    value unknown

    Returns

    boolean


    isBuiltInConstructor()

    Checks if a given value is a built-in JavaScript constructor.

    This function verifies whether the provided value is a function and matches one of JavaScript's built-in constructors, such as Object, Array, Function, etc.

    Param

    The value to check.

    Example

    console.log(isBuiltInConstructor(Object)); // Output: true
    console.log(isBuiltInConstructor(Array)); // Output: true
    console.log(isBuiltInConstructor(class MyClass {})); // Output: false
    console.log(isBuiltInConstructor(() => {})); // Output: false
    console.log(isBuiltInConstructor(123)); // Output: false

    Call Signature

    function isBuiltInConstructor(value): value is BuiltInConstructor;

    Defined in: main.ts:439

    Parameters

    Parameter Type
    value unknown

    Returns

    value is BuiltInConstructor

    Call Signature

    function isBuiltInConstructor(value): boolean;

    Defined in: main.ts:446

    Parameters

    Parameter Type
    value unknown

    Returns

    boolean


    isClass()

    Checks if a given value is a class constructor.

    This function determines whether the provided value is a class by verifying if it is a function and checking its prototype descriptor. Class constructors always have a non-writeable prototype, while regular functions do not.

    Will always return false on built in constructors like Date or Array.

    Param

    The value to check.

    Example

    class MyClass {}
    console.log(isClass(MyClass)); // Output: true
    
    function regularFunction() {}
    console.log(isClass(regularFunction)); // Output: false
    
    console.log(isClass(() => {})); // Output: false
    console.log(isClass(null)); // Output: false

    Call Signature

    function isClass(value): value is ClassCtor<any>;

    Defined in: main.ts:364

    Parameters

    Parameter Type
    value unknown

    Returns

    value is ClassCtor<any>

    Call Signature

    function isClass(value): boolean;

    Defined in: main.ts:369

    Parameters

    Parameter Type
    value unknown

    Returns

    boolean


    isDefined()

    Copy of isNotUndefined

    Param

    The value to check.

    Call Signature

    function isDefined<T>(value): value is Exclude<T, undefined>;

    Defined in: main.ts:165

    Type Parameters

    Type Parameter
    T

    Parameters

    Parameter Type
    value T

    Returns

    value is Exclude<T, undefined>

    Call Signature

    function isDefined(value): boolean;

    Defined in: main.ts:170

    Parameters

    Parameter Type
    value unknown

    Returns

    boolean


    isInstanceOfUnknownClass()

    Checks if a given value is an instance of a non-standard (unknown) class.

    This function determines whether the provided value is an object and has a prototype that is neither Object.prototype (standard object) nor null (no prototype). It helps differentiate between instances of custom classes and plain objects.

    Param

    The value to check.

    Example

    class MyClass {}
    console.log(isInstanceOfUnknownClass(new MyClass())); // Output: true
    console.log(isInstanceOfUnknownClass({})); // Output: false
    console.log(isInstanceOfUnknownClass(Object.create(null))); // Output: false
    console.log(isInstanceOfUnknownClass([])); // Output: true

    Call Signature

    function isInstanceOfUnknownClass(value): value is object;

    Defined in: main.ts:595

    Parameters

    Parameter Type
    value unknown

    Returns

    value is object

    Call Signature

    function isInstanceOfUnknownClass(value): boolean;

    Defined in: main.ts:600

    Parameters

    Parameter Type
    value unknown

    Returns

    boolean


    isNotBoolean()

    Checks if the given value is not a boolean.

    Param

    The value to check.

    Call Signature

    function isNotBoolean<T>(value): value is Exclude<T, boolean>;

    Defined in: main.ts:105

    Type Parameters

    Type Parameter
    T

    Parameters

    Parameter Type
    value T

    Returns

    value is Exclude<T, boolean>

    Call Signature

    function isNotBoolean(value): boolean;

    Defined in: main.ts:110

    Parameters

    Parameter Type
    value unknown

    Returns

    boolean


    isNotNumber()

    Checks if the given value is not a number.

    Param

    The value to check.

    Call Signature

    function isNotNumber<T>(value): value is Exclude<T, number>;

    Defined in: main.ts:65

    Type Parameters

    Type Parameter
    T

    Parameters

    Parameter Type
    value T

    Returns

    value is Exclude<T, number>

    Call Signature

    function isNotNumber(value): boolean;

    Defined in: main.ts:70

    Parameters

    Parameter Type
    value unknown

    Returns

    boolean


    isNotString()

    Checks if the given value is not a string.

    Param

    The value to check.

    Call Signature

    function isNotString<T>(value): value is Exclude<T, string>;

    Defined in: main.ts:25

    Type Parameters

    Type Parameter
    T

    Parameters

    Parameter Type
    value T

    Returns

    value is Exclude<T, string>

    Call Signature

    function isNotString(value): boolean;

    Defined in: main.ts:30

    Parameters

    Parameter Type
    value unknown

    Returns

    boolean


    isNotUndefined()

    Checks if the given value is not undefined.

    Param

    The value to check.

    Call Signature

    function isNotUndefined<T>(value): value is Exclude<T, undefined>;

    Defined in: main.ts:145

    Type Parameters

    Type Parameter
    T

    Parameters

    Parameter Type
    value T

    Returns

    value is Exclude<T, undefined>

    Call Signature

    function isNotUndefined(value): boolean;

    Defined in: main.ts:150

    Parameters

    Parameter Type
    value unknown

    Returns

    boolean


    isNumber()

    Checks if the given value is a number.

    Param

    The value to check.

    Call Signature

    function isNumber(value): value is number;

    Defined in: main.ts:45

    Parameters

    Parameter Type
    value unknown

    Returns

    value is number

    Call Signature

    function isNumber(value): boolean;

    Defined in: main.ts:50

    Parameters

    Parameter Type
    value unknown

    Returns

    boolean


    isObjectLoose()

    Checks if a given value is an object or a function.

    This function verifies whether the provided value is of type 'object' or 'function' while ensuring that null is excluded.

    Param

    The value to check.

    Example

    console.log(isObjectLoose({})); // Output: true
    console.log(isObjectLoose([])); // Output: true
    console.log(isObjectLoose(() => {})); // Output: true
    console.log(isObjectLoose(null)); // Output: false
    console.log(isObjectLoose(42)); // Output: false

    Features

    • ✅ Recognizes all objects (plain objects, arrays, functions, dates, etc.).
    • ✅ Recognizes functions as objects (since functions are technically objects in JavaScript).
    • ❌ Does not differentiate between plain objects and special objects (like arrays, functions, DOM nodes, etc.).

    Behaviour

    • isObjectLoose({})true
    • isObjectLoose([])true
    • isObjectLoose(() => {})true
    • isObjectLoose(null)false

    When to use

    • Use isObjectStrict when you need a strict check for plain objects.
    • Use isObjectLoose if you need to check if a value is an object-like structure, including functions.

    Comparison

    Feature Strict Check (isObjectStrict) Loose Check (isObjectLoose)
    Recognizes plain objects ✅ Yes ✅ Yes
    Recognizes functions ❌ No ✅ Yes
    Recognizes arrays ❌ No ✅ Yes
    Recognizes Object.create(null) objects ✅ Yes ✅ Yes
    Recognizes class instances ❌ No ✅ Yes
    Recognizes DOM elements ❌ No ✅ Yes
    Complexity 🔴 High 🟢 Low

    Call Signature

    function isObjectLoose(value): value is object;

    Defined in: main.ts:301

    Parameters

    Parameter Type
    value unknown

    Returns

    value is object

    Call Signature

    function isObjectLoose(value): boolean;

    Defined in: main.ts:306

    Parameters

    Parameter Type
    value unknown

    Returns

    boolean


    isObjectPlain()

    Determines whether a value is a plain object (i.e., created via an object literal, Object.create(null), or with Object as its prototype).

    This excludes arrays, functions, class instances, built-ins like Date/Map/Set, and other exotic objects.

    Param

    The value to test.

    Example

    const a: unknown = { x: 1 };
    const b: unknown = [];
    const c: unknown = new Date();
    const d: unknown = Object.create(null);
    
    isObjectPlain(a); // true
    isObjectPlain(b); // false (array)
    isObjectPlain(c); // false (built-in)
    isObjectPlain(d); // true (null prototype)
    
    // Type narrowing example:
    const value: unknown = { foo: 42 };
    if (isObjectPlain(value)) {
      // value is now Record<string, unknown>
      console.log(value.foo);
    }

    See

    Call Signature

    function isObjectPlain(value): value is Record<string, unknown>;

    Defined in: main.ts:185

    Parameters

    Parameter Type
    value unknown

    Returns

    value is Record<string, unknown>

    Call Signature

    function isObjectPlain(value): boolean;

    Defined in: main.ts:190

    Parameters

    Parameter Type
    value unknown

    Returns

    boolean


    isObjectStrict()

    Checks if a given value is a plain object.

    A plain object is an object created by the {} syntax, Object.create(null), or using new Object(). This function ensures that the value is an object and does not have an unusual prototype chain.

    Param

    The value to check.

    Example

    console.log(isObjectStrict({})); // Output: true
    console.log(isObjectStrict(Object.create(null))); // Output: true
    console.log(isObjectStrict([])); // Output: false
    console.log(isObjectStrict(new Date())); // Output: false
    console.log(isObjectStrict(null)); // Output: false

    Features

    • ✅ Recognizes only plain objects (created via {}, new Object(), Object.create(null), etc.).
    • ❌ Rejects arrays, functions, DOM elements, class instances, and custom objects with modified constructors.

    Behaviour

    • isObjectStrict({})true
    • isObjectStrict([])false
    • isObjectStrict(() => {})false
    • isObjectStrict(Object.create(null))true

    When to use

    • Use isObjectStrict when you need a strict check for plain objects.
    • Use isObjectLoose if you need to check if a value is an object-like structure, including functions.

    Call Signature

    function isObjectStrict(value): value is Record<string, unknown>;

    Defined in: main.ts:236

    Parameters

    Parameter Type
    value unknown

    Returns

    value is Record<string, unknown>

    Call Signature

    function isObjectStrict(value): boolean;

    Defined in: main.ts:243

    Parameters

    Parameter Type
    value unknown

    Returns

    boolean


    isString()

    Checks if the given value is a string.

    Param

    The value to check.

    Call Signature

    function isString(value): value is string;

    Defined in: main.ts:5

    Parameters

    Parameter Type
    value unknown

    Returns

    value is string

    Call Signature

    function isString(value): boolean;

    Defined in: main.ts:10

    Parameters

    Parameter Type
    value unknown

    Returns

    boolean


    isUndefined()

    Checks if the given value is undefined.

    Param

    The value to check.

    Call Signature

    function isUndefined(value): value is undefined;

    Defined in: main.ts:125

    Parameters

    Parameter Type
    value unknown

    Returns

    value is undefined

    Call Signature

    function isUndefined(value): boolean;

    Defined in: main.ts:130

    Parameters

    Parameter Type
    value unknown

    Returns

    boolean


    Development

    // Build
    $ npm run build
    // Run dev
    $ npm run dev
    // Test
    $ npm test

    Contributing

    Want to contribute? Please read the CONTRIBUTING.md and CODE_OF_CONDUCT.md

    License

    This project is licensed under the MIT License - see the LICENSE file for details.

    Changelog

    See the CHANGELOG.md for details on the latest updates.

    I'm an Open Source evangelist, creating stuff that does not exist yet to help get rid of secondary activities and to enhance systems already in place, be it documentation or web sites.

    The sponsorship is an unique opportunity to alleviate more hours for me to maintain my projects, create new ones and contribute to the large community we're all part of :)

    Support me on GitHub Sponsors.

    p.s. Ukraine is still under brutal Russian invasion. A lot of Ukrainian people are hurt, without shelter and need help. You can help in various ways, for instance, directly helping refugees, spreading awareness, putting pressure on your local government or companies. You can also support Ukraine by donating e.g. to Red Cross, Ukraine humanitarian organisation or donate Ambulances for Ukraine.