A set of JavaScript helper functions to check for types
npm install @phun-ky/typeof   !npm version !issues !license !size !npm !GitHub Repo stars  
A set of JavaScript helper functions to check for types.
- @phun-ky/typeof
- About
- Installation
- Usage
- API
- isBoolean()
- isBuiltInCallable()
- isBuiltInConstructor()
- isClass()
- isDefined()
- isFunction()
- isInstanceOfUnknownClass()
- isNotBoolean()
- isNotNumber()
- isNotString()
- isNotUndefined()
- isNumber()
- isObjectLoose()
- isObjectPlain()
- isObjectStrict()
- isString()
- isUndefined()
- Development
- Contributing
- License
- Changelog
- Sponsor me
``shell-session`
npm i --save @phun-ky/typeof
Either import and run the required functions:
`javascript
import { isString } from '@phun-ky/typeof';
isString('asd'); // true;
`
Checks if the given value is a boolean.
#### Param
The value to check.
#### Call Signature
`ts`
function isBoolean(value): value is boolean;
Defined in: main.ts:85
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
value is boolean
#### Call Signature
`ts`
function isBoolean(value): boolean;
Defined in: main.ts:90
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
boolean
---
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), orBigInt
- a callable non-constructable built-in (, 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.(v) => boolean
- Boolean: usable in contexts that require a plain .
#### Param
The value to check.
#### Example
`ts
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
`ts`
function isBuiltInCallable(value): value is BuiltInCallable;
Defined in: main.ts:541
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
value is BuiltInCallable
#### Call Signature
`ts`
function isBuiltInCallable(value): boolean;
Defined in: main.ts:546
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
boolean
---
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
`ts`
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
`ts`
function isBuiltInConstructor(value): value is BuiltInConstructor;
Defined in: main.ts:441
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
value is BuiltInConstructor
#### Call Signature
`ts`
function isBuiltInConstructor(value): boolean;
Defined in: main.ts:448
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
boolean
---
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-writable prototype, while regular functions do not.
Will always return false on built in constructors like Date or Array.
#### Param
The value to check.
#### Example
`ts
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
`ts`
function isClass(value): value is ClassCtor
Defined in: main.ts:366
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
value is ClassCtor
#### Call Signature
`ts`
function isClass(value): boolean;
Defined in: main.ts:371
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
boolean
---
Copy of isNotUndefined
#### Param
The value to check.
#### Call Signature
`ts`
function isDefined
Defined in: main.ts:166
##### Type Parameters
| Type Parameter |
| -------------- |
| T |
##### Parameters
| Parameter | Type |
| --------- | ---- |
| value | T |
##### Returns
value is Exclude
#### Call Signature
`ts`
function isDefined(value): boolean;
Defined in: main.ts:171
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
boolean
---
Checks if the given value is a function.
#### Param
The value to check.
#### Call Signature
`ts`
function isFunction(value): value is (args: unknown[]) => unknown;
Defined in: main.ts:635
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
value is (args: unknown[]) => unknown
#### Call Signature
`ts`
function isFunction(value): boolean;
Defined in: main.ts:642
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
boolean
---
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
`ts`
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
`ts`
function isInstanceOfUnknownClass(value): value is object;
Defined in: main.ts:597
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
value is object
#### Call Signature
`ts`
function isInstanceOfUnknownClass(value): boolean;
Defined in: main.ts:602
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
boolean
---
Checks if the given value is not a boolean.
#### Param
The value to check.
#### Call Signature
`ts`
function isNotBoolean
Defined in: main.ts:105
##### Type Parameters
| Type Parameter |
| -------------- |
| T |
##### Parameters
| Parameter | Type |
| --------- | ---- |
| value | T |
##### Returns
value is Exclude
#### Call Signature
`ts`
function isNotBoolean(value): boolean;
Defined in: main.ts:110
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
boolean
---
Checks if the given value is not a number.
#### Param
The value to check.
#### Call Signature
`ts`
function isNotNumber
Defined in: main.ts:65
##### Type Parameters
| Type Parameter |
| -------------- |
| T |
##### Parameters
| Parameter | Type |
| --------- | ---- |
| value | T |
##### Returns
value is Exclude
#### Call Signature
`ts`
function isNotNumber(value): boolean;
Defined in: main.ts:70
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
boolean
---
Checks if the given value is not a string.
#### Param
The value to check.
#### Call Signature
`ts`
function isNotString
Defined in: main.ts:25
##### Type Parameters
| Type Parameter |
| -------------- |
| T |
##### Parameters
| Parameter | Type |
| --------- | ---- |
| value | T |
##### Returns
value is Exclude
#### Call Signature
`ts`
function isNotString(value): boolean;
Defined in: main.ts:30
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
boolean
---
Checks if the given value is not undefined.
#### Param
The value to check.
#### Call Signature
`ts`
function isNotUndefined
Defined in: main.ts:145
##### Type Parameters
| Type Parameter |
| -------------- |
| T |
##### Parameters
| Parameter | Type |
| --------- | ---- |
| value | T |
##### Returns
value is Exclude
#### Call Signature
`ts`
function isNotUndefined(value): boolean;
Defined in: main.ts:150
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
boolean
---
Checks if the given value is a number.
#### Param
The value to check.
#### Call Signature
`ts`
function isNumber(value): value is number;
Defined in: main.ts:45
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
value is number
#### Call Signature
`ts`
function isNumber(value): boolean;
Defined in: main.ts:50
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
boolean
---
Checks if a given value is an object or a function.
This function verifies whether the provided value is of type 'object' or 'function'null
while ensuring that is excluded.
#### Param
The value to check.
#### Example
`ts`
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.).
Behavior
- ✅ isObjectLoose({}) → trueisObjectLoose([])
- ✅ → trueisObjectLoose(() => {})
- ✅ → trueisObjectLoose(null)
- ❌ → false
When to use
- Use isObjectStrict when you need a strict check for plain objects.isObjectLoose
- Use if you need to check if a value is an object-like structure, including functions.
Comparison
| Feature | Strict Check (isObjectStrict) | Loose Check (isObjectLoose) |Object.create(null)
| ---------------------------------------- | ------------------------------- | ----------------------------- |
| Recognizes plain objects | ✅ Yes | ✅ Yes |
| Recognizes functions | ❌ No | ✅ Yes |
| Recognizes arrays | ❌ No | ✅ Yes |
| Recognizes objects | ✅ Yes | ✅ Yes |
| Recognizes class instances | ❌ No | ✅ Yes |
| Recognizes DOM elements | ❌ No | ✅ Yes |
| Complexity | 🔴 High | 🟢 Low |
#### Call Signature
`ts`
function isObjectLoose(value): value is object;
Defined in: main.ts:303
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
value is object
#### Call Signature
`ts`
function isObjectLoose(value): boolean;
Defined in: main.ts:308
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
boolean
---
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
`ts
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
console.log(value.foo);
}
`
#### See
-
-
#### Call Signature
`ts`
function isObjectPlain(value): value is Record
Defined in: main.ts:187
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
value is Record
#### Call Signature
`ts`
function isObjectPlain(value): boolean;
Defined in: main.ts:192
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
boolean
---
Checks if a given value is a plain object.
A plain object is an object created by the {} syntax, Object.create(null),new Object()
or using . This function ensures that the value is an object
and does not have an unusual prototype chain.
#### Param
The value to check.
#### Example
`ts`
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.
Behavior
- ✅ isObjectStrict({}) → trueisObjectStrict([])
- ❌ → falseisObjectStrict(() => {})
- ❌ → falseisObjectStrict(Object.create(null))
- ✅ → true
When to use
- Use isObjectStrict when you need a strict check for plain objects.isObjectLoose
- Use if you need to check if a value is an object-like structure, including functions.
#### Call Signature
`ts`
function isObjectStrict(value): value is Record
Defined in: main.ts:238
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
value is Record
#### Call Signature
`ts`
function isObjectStrict(value): boolean;
Defined in: main.ts:245
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
boolean
---
Checks if the given value is a string.
#### Param
The value to check.
#### Call Signature
`ts`
function isString(value): value is string;
Defined in: main.ts:5
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
value is string
#### Call Signature
`ts`
function isString(value): boolean;
Defined in: main.ts:10
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
boolean
---
Checks if the given value is undefined.
#### Param
The value to check.
#### Call Signature
`ts`
function isUndefined(value): value is undefined;
Defined in: main.ts:125
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
value is undefined
#### Call Signature
`ts`
function isUndefined(value): boolean;
Defined in: main.ts:130
##### Parameters
| Parameter | Type |
| --------- | --------- |
| value | unknown |
##### Returns
boolean
---
`shell-session``
// Build
$ npm run build
// Run dev
$ npm run dev
// Test
$ npm test
Want to contribute? Please read the CONTRIBUTING.md and CODE_OF_CONDUCT.md
This project is licensed under the MIT License - see the LICENSE file for details.
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.