guardz
Version:
A simple and lightweight TypeScript type guard library for runtime type validation.
37 lines (36 loc) • 1.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isString = void 0;
const reportTypeGuardError_1 = require("../utils/reportTypeGuardError");
/**
* Checks if a value is a string.
*
* @param value - The value to check
* @param config - Optional configuration for error handling
* @returns True if the value is a string, false otherwise
*
* @example
* ```typescript
* import { isString } from 'guardz';
*
* console.log(isString("hello")); // true
* console.log(isString("")); // true
* console.log(isString(123)); // false
* console.log(isString(null)); // false
*
* // With type narrowing
* const data: unknown = getUserInput();
* if (isString(data)) {
* // data is now typed as string
* console.log(data.toUpperCase());
* }
* ```
*/
const isString = function isStringGuard(value, config) {
if (typeof value !== 'string') {
(0, reportTypeGuardError_1.reportTypeGuardError)(config, value, 'string');
return false;
}
return true;
};
exports.isString = isString;