Skip to content

isObject

Determine if a value is an Object

128 bytes
since v12.1.0

Usage

Pass in a value and get a boolean telling you if the value is a plain object, a custom class instance, or an object with a null prototype.

This function returns false for arrays, functions, and built-in object types like Date, RegExp, Map, and Set.

import * as
import _
_
from 'radashi'
class
class Data
Data
{}
import _
_
.
function isObject(value: unknown): value is object

Returns true if value is a plain object, a class instance (excluding built-in classes like Date/RegExp), or an Object.create(null) result. Objects from other realms are also supported.

Use this as a TypeScript type guard only when the input is known to be either an object accepted by isObject or a non-object value. The object type in TypeScript also includes arrays, functions, dates, maps, sets, and other object-like values that isObject may reject at runtime.

@seehttps://radashi.js.org/reference/typed/isObject

@example

isObject({}) // true
isObject(new Object()) // true
isObject(Object.create(null)) // true
isObject(new class {}) // true
isObject([]) // false
isObject(/.+/g) // false
isObject(new Date()) // false
isObject(new Map()) // false
isObject(new Set()) // false

@version12.1.0

isObject
({}) // => true
import _
_
.
function isObject(value: unknown): value is object

Returns true if value is a plain object, a class instance (excluding built-in classes like Date/RegExp), or an Object.create(null) result. Objects from other realms are also supported.

Use this as a TypeScript type guard only when the input is known to be either an object accepted by isObject or a non-object value. The object type in TypeScript also includes arrays, functions, dates, maps, sets, and other object-like values that isObject may reject at runtime.

@seehttps://radashi.js.org/reference/typed/isObject

@example

isObject({}) // true
isObject(new Object()) // true
isObject(Object.create(null)) // true
isObject(new class {}) // true
isObject([]) // false
isObject(/.+/g) // false
isObject(new Date()) // false
isObject(new Map()) // false
isObject(new Set()) // false

@version12.1.0

isObject
(
var Object: ObjectConstructor

Provides functionality common to all JavaScript objects.

Object
.
ObjectConstructor.create(o: object | null): any (+1 overload)

Creates an object that has the specified prototype or that has null prototype.

@paramo Object to use as a prototype. May be null.

create
(null)) // => true
import _
_
.
function isObject(value: unknown): value is object

Returns true if value is a plain object, a class instance (excluding built-in classes like Date/RegExp), or an Object.create(null) result. Objects from other realms are also supported.

Use this as a TypeScript type guard only when the input is known to be either an object accepted by isObject or a non-object value. The object type in TypeScript also includes arrays, functions, dates, maps, sets, and other object-like values that isObject may reject at runtime.

@seehttps://radashi.js.org/reference/typed/isObject

@example

isObject({}) // true
isObject(new Object()) // true
isObject(Object.create(null)) // true
isObject(new class {}) // true
isObject([]) // false
isObject(/.+/g) // false
isObject(new Date()) // false
isObject(new Map()) // false
isObject(new Set()) // false

@version12.1.0

isObject
(new
constructor Data(): Data
Data
()) // => true
import _
_
.
function isObject(value: unknown): value is object

Returns true if value is a plain object, a class instance (excluding built-in classes like Date/RegExp), or an Object.create(null) result. Objects from other realms are also supported.

Use this as a TypeScript type guard only when the input is known to be either an object accepted by isObject or a non-object value. The object type in TypeScript also includes arrays, functions, dates, maps, sets, and other object-like values that isObject may reject at runtime.

@seehttps://radashi.js.org/reference/typed/isObject

@example

isObject({}) // true
isObject(new Object()) // true
isObject(Object.create(null)) // true
isObject(new class {}) // true
isObject([]) // false
isObject(/.+/g) // false
isObject(new Date()) // false
isObject(new Map()) // false
isObject(new Set()) // false

@version12.1.0

isObject
([]) // => false
import _
_
.
function isObject(value: unknown): value is object

Returns true if value is a plain object, a class instance (excluding built-in classes like Date/RegExp), or an Object.create(null) result. Objects from other realms are also supported.

Use this as a TypeScript type guard only when the input is known to be either an object accepted by isObject or a non-object value. The object type in TypeScript also includes arrays, functions, dates, maps, sets, and other object-like values that isObject may reject at runtime.

@seehttps://radashi.js.org/reference/typed/isObject

@example

isObject({}) // true
isObject(new Object()) // true
isObject(Object.create(null)) // true
isObject(new class {}) // true
isObject([]) // false
isObject(/.+/g) // false
isObject(new Date()) // false
isObject(new Map()) // false
isObject(new Set()) // false

@version12.1.0

isObject
(new
var Date: DateConstructor
new () => Date (+3 overloads)
Date
()) // => false
import _
_
.
function isObject(value: unknown): value is object

Returns true if value is a plain object, a class instance (excluding built-in classes like Date/RegExp), or an Object.create(null) result. Objects from other realms are also supported.

Use this as a TypeScript type guard only when the input is known to be either an object accepted by isObject or a non-object value. The object type in TypeScript also includes arrays, functions, dates, maps, sets, and other object-like values that isObject may reject at runtime.

@seehttps://radashi.js.org/reference/typed/isObject

@example

isObject({}) // true
isObject(new Object()) // true
isObject(Object.create(null)) // true
isObject(new class {}) // true
isObject([]) // false
isObject(/.+/g) // false
isObject(new Date()) // false
isObject(new Map()) // false
isObject(new Set()) // false

@version12.1.0

isObject
(() => {}) // => false
import _
_
.
function isObject(value: unknown): value is object

Returns true if value is a plain object, a class instance (excluding built-in classes like Date/RegExp), or an Object.create(null) result. Objects from other realms are also supported.

Use this as a TypeScript type guard only when the input is known to be either an object accepted by isObject or a non-object value. The object type in TypeScript also includes arrays, functions, dates, maps, sets, and other object-like values that isObject may reject at runtime.

@seehttps://radashi.js.org/reference/typed/isObject

@example

isObject({}) // true
isObject(new Object()) // true
isObject(Object.create(null)) // true
isObject(new class {}) // true
isObject([]) // false
isObject(/.+/g) // false
isObject(new Date()) // false
isObject(new Map()) // false
isObject(new Set()) // false

@version12.1.0

isObject
(null) // => false

TypeScript narrowing

This is a good fit for accepting either an options object or a primitive shorthand.

import * as
import _
_
from 'radashi'
type
type Options = {
foo?: string;
}
Options
= {
foo?: string
foo
?: string }
declare let
let value: string | Options | undefined
value
:
type Options = {
foo?: string;
}
Options
| string | undefined
const
const options: Options
options
:
type Options = {
foo?: string;
}
Options
=
import _
_
.
function isObject(value: unknown): value is object

Returns true if value is a plain object, a class instance (excluding built-in classes like Date/RegExp), or an Object.create(null) result. Objects from other realms are also supported.

Use this as a TypeScript type guard only when the input is known to be either an object accepted by isObject or a non-object value. The object type in TypeScript also includes arrays, functions, dates, maps, sets, and other object-like values that isObject may reject at runtime.

@seehttps://radashi.js.org/reference/typed/isObject

@example

isObject({}) // true
isObject(new Object()) // true
isObject(Object.create(null)) // true
isObject(new class {}) // true
isObject([]) // false
isObject(/.+/g) // false
isObject(new Date()) // false
isObject(new Map()) // false
isObject(new Set()) // false

@version12.1.0

isObject
(
let value: string | Options | undefined
value
)
?
let value: Options
value
:
let value: string | undefined
value
===
var undefined
undefined
? {}
: {
foo?: string
foo
:
let value: string
value
}

If a union can contain arrays, functions, or special objects, narrow those cases directly instead of starting with _.isObject.

import * as
import _
_
from 'radashi'
type
type Options = {
foo?: string;
}
Options
= {
foo?: string
foo
?: string }
declare const
const value: Options | (() => Options) | Options[]
value
:
type Options = {
foo?: string;
}
Options
| (() =>
type Options = {
foo?: string;
}
Options
) |
type Options = {
foo?: string;
}
Options
[]
if (
import _
_
.
function isObject(value: unknown): value is object

Returns true if value is a plain object, a class instance (excluding built-in classes like Date/RegExp), or an Object.create(null) result. Objects from other realms are also supported.

Use this as a TypeScript type guard only when the input is known to be either an object accepted by isObject or a non-object value. The object type in TypeScript also includes arrays, functions, dates, maps, sets, and other object-like values that isObject may reject at runtime.

@seehttps://radashi.js.org/reference/typed/isObject

@example

isObject({}) // true
isObject(new Object()) // true
isObject(Object.create(null)) // true
isObject(new class {}) // true
isObject([]) // false
isObject(/.+/g) // false
isObject(new Date()) // false
isObject(new Map()) // false
isObject(new Set()) // false

@version12.1.0

isObject
(
const value: Options | (() => Options) | Options[]
value
)) {
value
const value: Options | (() => Options) | Options[]
} else {
value
const value: never
}
if (
import _
_
.
const isArray: <Options | (() => Options) | Options[]>(value: Options | (() => Options) | Options[]) => value is Options[]

Literally just Array.isArray but with better type inference.

@seehttps://radashi.js.org/reference/typed/isArray

@example

isArray([]) // => true
isArray('hello') // => false

@version12.1.0

isArray
(
const value: Options | (() => Options) | Options[]
value
)) {
value
const value: Options[]
} else if (
import _
_
.
function isFunction(value: any): value is Function

Return true if the given value is a function.

@seehttps://radashi.js.org/reference/typed/isFunction

@example

isFunction(0) // => false
isFunction(() => {}) // => true
isFunction(function() {}) // => true
isFunction(async function() {}) // => true
isFunction(class {}) // => false

@version12.1.0

isFunction
(
const value: Options | (() => Options)
value
)) {
value
const value: () => Options
} else {
value
const value: Options
}