Skip to content

isPlainObject

Edit

Checks for object literals and null-prototype records.

isPlainObject.ts
/** Checks whether a value is an object literal or has a null prototype. */
export const isPlainObject = (value: unknown): value is Record<PropertyKey, unknown> => {
if (value === null || typeof value !== "object") return false;
const prototype = Object.getPrototypeOf(value);
return prototype === null || prototype === Object.prototype;
};
Terminal
wget -O src/lib/isPlainObject.ts https://raw.githubusercontent.com/jrTilak/lazykit/HEAD/registry/functions/isPlainObject.ts
isPlainObject.example.ts
import { isPlainObject } from "./isPlainObject";
const value: unknown = JSON.parse('{"status":"ready"}');
if (isPlainObject(value)) {
console.log(value.status);
}
  • value (unknown) — Candidate value.
  • Returns a type predicate for Record<PropertyKey, unknown>.
  • Arrays, dates, functions, and class instances return false.