Function isOrdinaryObject

  • Type Parameters

    • T extends object

    Parameters

    • value: T

    Returns value is T

  • Runtime check for whether a value is an ordinary object:

    An ordinary object in this context is a non-null, non-callable object for which Object.prototype.toString.call(value) returns '[object Object]'.

    This includes:

    • Object literals created with {}.
    • Objects created with new Object().
    • Objects with a null prototype.
    • Objects with a custom prototype.
    • Instances of ordinary user-defined classes.

    This excludes:

    • Arrays.
    • Functions and class constructors.
    • Primitive and boxed primitive values.
    • Specialized built-in objects such as Date, RegExp, Map, Set, Promise, Error, ArrayBuffer, DataView, and typed arrays.

    This predicate occupies the middle ground between the other object predicates:

    • isObject accepts the broader category of non-null, non-array objects, including specialized built-ins.
    • isRecord accepts the same broad record-like category, but narrows the result for dictionary-style keyed access.
    • isOrdinaryObject additionally requires the runtime string tag '[object Object]'. It accepts class instances and objects with custom prototypes, but rejects specialized built-ins.
    • isPlainObject requires the prototype to be exactly Object.prototype or null. It therefore rejects class instances and objects with other custom prototypes.

    Unlike isPlainObject, this function does not inspect or restrict the object's prototype.

    Parameters

    • value: unknown

      Any value to evaluate.

    Returns value is Record<PropertyKey, unknown>

    Whether value is a non-null object with the runtime string tag '[object Object]'.

    This is a tag-based classification and is not an implementation of the ECMAScript specification's internal distinction between ordinary and exotic objects.

    The result of Object.prototype.toString.call can be influenced by Symbol.toStringTag. Consequently, an object may opt out of this classification by supplying another tag, and a specialized object may present itself with the tag 'Object'.

    isOrdinaryObject({ value: 1 });             // true
    isOrdinaryObject(Object.create(null)); // true

    class Configuration {}
    isOrdinaryObject(new Configuration()); // true

    isOrdinaryObject(new Map()); // false
    isOrdinaryObject(new Date()); // false
    isOrdinaryObject([]); // false

    The distinction from isPlainObject concerns the prototype:

    class Configuration {}

    const value = new Configuration();

    isOrdinaryObject(value); // true
    isPlainObject(value); // false

    Symbol.toStringTag can alter the result:

    const value = {
    [Symbol.toStringTag]: 'Configuration'
    };

    isOrdinaryObject(value); // false: '[object Configuration]'