Skip to content

Instantly share code, notes, and snippets.

@ryangoree
Created June 9, 2026 16:33
Show Gist options
  • Select an option

  • Save ryangoree/4ed6fc2fe284b19c5b5a33bc7476870a to your computer and use it in GitHub Desktop.

Select an option

Save ryangoree/4ed6fc2fe284b19c5b5a33bc7476870a to your computer and use it in GitHub Desktop.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type AnyObject = Record<PropertyKey, any>;
/**
* Detect whether `T` is a plain, record-like object (e.g., `{ a: string }`),
* excluding functions, arrays/tuples, and common built-ins (`Map`, `Set`,
* `Date`, etc.).
*
* @example
* ```ts
* type A = IsPlainObject<{ a: string }>; // true
* type B = IsPlainObject<() => void>; // false
* type C = IsPlainObject<string[]>; // false
* type D = IsPlainObject<Map<string, number>>; // false
* type E = IsPlainObject<Date>; // false
* type F = IsPlainObject<string | { a: 1 }>; // boolean
* ```
*/
export type IsPlainObject<T> = [T] extends [never]
? false
: T extends
| ((...args: never) => unknown)
| readonly unknown[]
| Date
| RegExp
| Promise<unknown>
| ReadonlySet<unknown> // Will also match ReadonlyMap
| WeakSet<WeakKey>
| WeakMap<WeakKey, unknown>
? false
: T extends object
? true
: false;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment