Détail du package

@phun-ky/typeof

phun-ky360.8kMIT2.0.2

A set of JavaScript helper functions to check for types

types, string, number, object

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.

Sponsor me

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.

changelog

Changelog

2.0.1 (2025-09-05)

Tasks

  • 🤖 bump the minor-and-patch group with 4 updates (17fc043)

2.0.0 (2025-09-02)

⚠ BREAKING CHANGES

  • 🧨 You can no longer use isBuiltInConstructor to check for BigInt or Symbol, use isBuiltInCallable(BigInt) || isBuiltInCallable(Symbol) instead.

Feature

  • 🎸 Add isBuiltInCallable, add type-guard overloads and refactor helpers to function declarations. (ae2bffd)

1.3.0 (2025-09-02)

Tasks

  • 🤖 Install missing node types (4315bc1)

Feature

  • 🎸 Add isObjectPlain for checking plain objects (a3c6a5f)

1.2.27 (2025-08-26)

Tasks

  • 🤖 bump the minor-and-patch group with 8 updates (26296ec)

1.2.26 (2025-08-19)

Tasks

  • 🤖 bump the minor-and-patch group with 7 updates (117b300)

1.2.25 (2025-08-12)

Tasks

  • 🤖 bump the minor-and-patch group with 4 updates (af49b2c)

1.2.24 (2025-08-05)

Tasks

  • 🤖 bump the minor-and-patch group with 6 updates (19371d4)

1.2.23 (2025-07-29)

Tasks

  • 🤖 bump the minor-and-patch group with 8 updates (bc858e9)

1.2.22 (2025-07-22)

Tasks

  • 🤖 bump the minor-and-patch group with 5 updates (d32abe5)

1.2.21 (2025-07-20)

Tasks

  • 🤖 bump the minor-and-patch group with 3 updates (be77a62)

1.2.20 (2025-07-07)

Tasks

  • 🤖 bump the minor-and-patch group across 1 directory with 7 updates (a1a2700)

1.2.19 (2025-06-23)

Tasks

  • 🤖 bump the minor-and-patch group with 4 updates (94d9ad6)

1.2.18 (2025-06-17)

Tasks

  • 🤖 bump the minor-and-patch group with 6 updates (59ba00a)

1.2.17 (2025-06-09)

Tasks

  • 🤖 bump rollup from 4.41.1 to 4.42.0 in the minor-and-patch group (05ad4bf)

1.2.16 (2025-06-02)

Tasks

  • 🤖 bump the minor-and-patch group with 6 updates (2636a35)

1.2.15 (2025-05-26)

Tasks

  • 🤖 bump the minor-and-patch group with 4 updates (2ed3123)

1.2.14 (2025-05-20)

Tasks

  • 🤖 bump the minor-and-patch group with 3 updates (698da9f)

1.2.13 (2025-05-13)

Tasks

  • 🤖 bump the minor-and-patch group with 3 updates (350df2c)

Bug

  • 🐛 Give proper permission to workflow (be06f4d)
  • 🐛 Labels (0d8d223)

1.2.12 (2025-05-06)

Tasks

  • 🤖 Add new dep for eslint config (96daac4)

1.2.11 (2025-05-05)

Tasks

  • 🤖 bump the minor-and-patch group with 2 updates (3f36579)
  • 🤖 Disable rules for markdown files (b12bea7)

1.2.10 (2025-05-04)

Tasks

Documentation

  • ✏️ Typography fixes (0885a88)

1.2.9 (2025-05-04)

Tasks

  • 🤖 Add ESLint config from @phun-ky/eslint-config (8963b92)

1.2.8 (2025-05-04)

Documentation

1.2.7 (2025-04-29)

Tasks

  • 🤖 bump the major-updates group across 1 directory with 3 updates (7f6ebb6)

1.2.6 (2025-04-29)

Tasks

  • 🤖 bump the minor-and-patch group with 10 updates (4c17eb6)

1.2.5 (2025-04-15)

Tasks

  • 🤖 bump the minor-and-patch group with 16 updates (752f3dc)

1.2.4 (2025-04-08)

Tasks

  • 🤖 bump the major-updates group across 1 directory with 11 updates (6810bbe)

1.2.3 (2025-03-03)

Tasks

  • 🤖 bump the minor-and-patch group with 5 updates (d7e72d7)

1.2.2 (2025-02-26)

Tasks

  • 🤖 Support node v20 as well (c31637c)

1.2.1 (2025-02-25)

Tasks

1.2.0 (2025-02-25)

Feature

  • 🎸 Add isInstanceOfUnknownClass (e42737f)

1.1.0 (2025-02-25)

Tasks

  • 🤖 Upgrade to eslint@v9 (a413fd3)

Feature

  • 🎸 Add checks for class/constructor and objects (555cab3)

Bug

  • 🐛 Use correct script name. (d14f466), closes #20

1.0.4 (2025-02-17)

Tasks

  • 🤖 bump the minor-and-patch group with 12 updates (4706034)

1.0.3 (2024-11-22)

Bug

1.0.2 (2024-11-22)

Documentation

  • ✏️ Add CHANGELOG.md (59e1082)

1.0.1 (2024-11-22)

Tasks

Documentation