Created
June 9, 2026 16:33
-
-
Save ryangoree/4ed6fc2fe284b19c5b5a33bc7476870a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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