$ cnpm install @phun-ky/typeof
A set of JavaScript helper functions to check for types.
npm i --save @phun-ky/typeof
Either import and run the required functions:
import { isString } from '@phun-ky/typeof';
isString('asd'); // true;
Checks if the given value is a boolean.
The value to check.
function isBoolean(value): value is boolean;
Defined in: main.ts:85
| Parameter | Type |
|---|---|
value |
unknown |
value is boolean
function isBoolean(value): boolean;
Defined in: main.ts:90
| Parameter | Type |
|---|---|
value |
unknown |
boolean
Checks if a given value is a built-in JavaScript callable.
A built-in callable is either:
Object, Array, Date, Map), orBigInt, Symbol).This function first verifies the value is a function, then tests identity against a curated set of built-ins.
Overloads:
BuiltInCallable on success.(v) => boolean.The value to check.
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);
}
function isBuiltInCallable(value): value is BuiltInCallable;
Defined in: main.ts:541
| Parameter | Type |
|---|---|
value |
unknown |
value is BuiltInCallable
function isBuiltInCallable(value): boolean;
Defined in: main.ts:546
| Parameter | Type |
|---|---|
value |
unknown |
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.
The value to check.
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
function isBuiltInConstructor(value): value is BuiltInConstructor;
Defined in: main.ts:441
| Parameter | Type |
|---|---|
value |
unknown |
value is BuiltInConstructor
function isBuiltInConstructor(value): boolean;
Defined in: main.ts:448
| Parameter | Type |
|---|---|
value |
unknown |
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.
The value to check.
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
function isClass(value): value is ClassCtor<any>;
Defined in: main.ts:366
| Parameter | Type |
|---|---|
value |
unknown |
value is ClassCtor<any>
function isClass(value): boolean;
Defined in: main.ts:371
| Parameter | Type |
|---|---|
value |
unknown |
boolean
Copy of isNotUndefined
The value to check.
function isDefined<T>(value): value is Exclude<T, undefined>;
Defined in: main.ts:166
| Type Parameter |
|---|
T |
| Parameter | Type |
|---|---|
value |
T |
value is Exclude<T, undefined>
function isDefined(value): boolean;
Defined in: main.ts:171
| Parameter | Type |
|---|---|
value |
unknown |
boolean
Checks if the given value is a function.
The value to check.
function isFunction(value): value is (args: unknown[]) => unknown;
Defined in: main.ts:635
| Parameter | Type |
|---|---|
value |
unknown |
value is (args: unknown[]) => unknown
function isFunction(value): boolean;
Defined in: main.ts:642
| Parameter | Type |
|---|---|
value |
unknown |
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.
The value to check.
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
function isInstanceOfUnknownClass(value): value is object;
Defined in: main.ts:597
| Parameter | Type |
|---|---|
value |
unknown |
value is object
function isInstanceOfUnknownClass(value): boolean;
Defined in: main.ts:602
| Parameter | Type |
|---|---|
value |
unknown |
boolean
Checks if the given value is not a boolean.
The value to check.
function isNotBoolean<T>(value): value is Exclude<T, boolean>;
Defined in: main.ts:105
| Type Parameter |
|---|
T |
| Parameter | Type |
|---|---|
value |
T |
value is Exclude<T, boolean>
function isNotBoolean(value): boolean;
Defined in: main.ts:110
| Parameter | Type |
|---|---|
value |
unknown |
boolean
Checks if the given value is not a number.
The value to check.
function isNotNumber<T>(value): value is Exclude<T, number>;
Defined in: main.ts:65
| Type Parameter |
|---|
T |
| Parameter | Type |
|---|---|
value |
T |
value is Exclude<T, number>
function isNotNumber(value): boolean;
Defined in: main.ts:70
| Parameter | Type |
|---|---|
value |
unknown |
boolean
Checks if the given value is not a string.
The value to check.
function isNotString<T>(value): value is Exclude<T, string>;
Defined in: main.ts:25
| Type Parameter |
|---|
T |
| Parameter | Type |
|---|---|
value |
T |
value is Exclude<T, string>
function isNotString(value): boolean;
Defined in: main.ts:30
| Parameter | Type |
|---|---|
value |
unknown |
boolean
Checks if the given value is not undefined.
The value to check.
function isNotUndefined<T>(value): value is Exclude<T, undefined>;
Defined in: main.ts:145
| Type Parameter |
|---|
T |
| Parameter | Type |
|---|---|
value |
T |
value is Exclude<T, undefined>
function isNotUndefined(value): boolean;
Defined in: main.ts:150
| Parameter | Type |
|---|---|
value |
unknown |
boolean
Checks if the given value is a number.
The value to check.
function isNumber(value): value is number;
Defined in: main.ts:45
| Parameter | Type |
|---|---|
value |
unknown |
value is number
function isNumber(value): boolean;
Defined in: main.ts:50
| Parameter | Type |
|---|---|
value |
unknown |
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'
while ensuring that null is excluded.
The value to check.
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
Behavior
isObjectLoose({}) → trueisObjectLoose([]) → trueisObjectLoose(() => {}) → trueisObjectLoose(null) → falseWhen to use
isObjectStrict when you need a strict check for plain objects.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 |
function isObjectLoose(value): value is object;
Defined in: main.ts:303
| Parameter | Type |
|---|---|
value |
unknown |
value is object
function isObjectLoose(value): boolean;
Defined in: main.ts:308
| Parameter | Type |
|---|---|
value |
unknown |
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.
The value to test.
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);
}
function isObjectPlain(value): value is Record<string, unknown>;
Defined in: main.ts:187
| Parameter | Type |
|---|---|
value |
unknown |
value is Record<string, unknown>
function isObjectPlain(value): boolean;
Defined in: main.ts:192
| Parameter | Type |
|---|---|
value |
unknown |
boolean
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.
The value to check.
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
{}, new Object(), Object.create(null), etc.).Behavior
isObjectStrict({}) → trueisObjectStrict([]) → falseisObjectStrict(() => {}) → falseisObjectStrict(Object.create(null)) → trueWhen to use
isObjectStrict when you need a strict check for plain objects.isObjectLoose if you need to check if a value is an object-like structure, including functions.function isObjectStrict(value): value is Record<string, unknown>;
Defined in: main.ts:238
| Parameter | Type |
|---|---|
value |
unknown |
value is Record<string, unknown>
function isObjectStrict(value): boolean;
Defined in: main.ts:245
| Parameter | Type |
|---|---|
value |
unknown |
boolean
Checks if the given value is a string.
The value to check.
function isString(value): value is string;
Defined in: main.ts:5
| Parameter | Type |
|---|---|
value |
unknown |
value is string
function isString(value): boolean;
Defined in: main.ts:10
| Parameter | Type |
|---|---|
value |
unknown |
boolean
Checks if the given value is undefined.
The value to check.
function isUndefined(value): value is undefined;
Defined in: main.ts:125
| Parameter | Type |
|---|---|
value |
unknown |
value is undefined
function isUndefined(value): boolean;
Defined in: main.ts:130
| Parameter | Type |
|---|---|
value |
unknown |
boolean
// 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.
Copyright 2013 - present © cnpmjs.org | Home |