UNPKG

guardz

Version:

A simple and lightweight TypeScript type guard library for runtime type validation.

31 lines (30 loc) 1.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isPick = isPick; const isNonNullObject_1 = require("./isNonNullObject"); /** * Creates a type guard that checks if a value is an object that contains * at least the selected subset of properties (picked keys) from a type. * * Notes: * - This utility is designed to be composed with a full object type guard * (e.g., from `isType`/`isSchema`). It ensures the specified keys exist. * - Validation of the picked properties' value types should be ensured by the * provided base type guard if you call it beforehand or compose it externally. */ function isPick(_baseTypeGuard, ...pickedKeys) { return function (value, config) { if (!(0, isNonNullObject_1.isNonNullObject)(value, config)) { return false; } for (const key of pickedKeys) { if (!Object.prototype.hasOwnProperty.call(value, key)) { // Delegate to base type guard to produce the correct expected type message if (config) _baseTypeGuard(value, config); return false; } } return true; }; }