UNPKG

ts-data-forge

Version:

[![npm version](https://img.shields.io/npm/v/ts-data-forge.svg)](https://www.npmjs.com/package/ts-data-forge) [![npm downloads](https://img.shields.io/npm/dm/ts-data-forge.svg)](https://www.npmjs.com/package/ts-data-forge) [![License](https://img.shields.

39 lines (37 loc) 1.43 kB
/** * Type guard that checks if a value is a non-null object. * * This function checks if a value has type `"object"` according to the `typeof` * operator and is not `null`. This includes all object types such as plain * objects, arrays, dates, regular expressions, and other object instances, but * excludes functions. * * **Type Narrowing Behavior:** * * - Narrows `unknown` to `object` * - Excludes `null`, `undefined`, and all primitive types * - Excludes functions (they have `typeof` === `"function"`, not `"object"`) * - Includes arrays, dates, regex, and other object instances * * **Note:** This function returns `true` for arrays. If you need to check for * plain objects specifically (excluding arrays), use `isRecord()` instead. * * @example * * ```ts * const mixed: unknown[] = [{ id: 1 }, null, 'Ada'] as const; * * const objects = mixed.filter(isNonNullObject); * * assert.deepStrictEqual(objects, [{ id: 1 }]); * ``` * * @param u - The value to check * @returns `true` if `u` is an object and not `null`, `false` otherwise. When * `true`, TypeScript narrows the type to `object`. * @see {@link isRecord} - For checking plain objects specifically (excludes arrays) */ // eslint-disable-next-line @typescript-eslint/no-restricted-types const isNonNullObject = (u) => typeof u === 'object' && u !== null; export { isNonNullObject }; //# sourceMappingURL=is-non-null-object.mjs.map