UNPKG

guardz

Version:

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

41 lines (40 loc) 1.55 kB
import type { TypeGuardFn } from './isType'; /** * Checks if a value is a URLSearchParams object. * * This type guard validates that a value is a URLSearchParams instance, which is available * in all modern JavaScript environments. * * @param value - The value to check * @param config - Optional configuration for error handling * @returns True if the value is a URLSearchParams object, false otherwise * * @example * ```typescript * import { isURLSearchParams } from 'guardz'; * * console.log(isURLSearchParams(new URLSearchParams())); // true * console.log(isURLSearchParams(new URLSearchParams('?name=john&age=30'))); // true * console.log(isURLSearchParams(new URLSearchParams({ name: 'john', age: '30' }))); // true * console.log(isURLSearchParams('name=john&age=30')); // false (string, not URLSearchParams) * console.log(isURLSearchParams({ name: 'john' })); // false (object, not URLSearchParams) * * // With type narrowing * const data: unknown = getQueryParams(); * if (isURLSearchParams(data)) { * // data is now typed as URLSearchParams * console.log('Name:', data.get('name')); * console.log('Age:', data.get('age')); * data.append('newParam', 'value'); * } * * // With error handling * const queryData: unknown = getQueryParams(); * if (!isURLSearchParams(queryData, { identifier: 'queryParams' })) { * console.error('Invalid query parameters'); * return; * } * // queryData is now typed as URLSearchParams * ``` */ export declare const isURLSearchParams: TypeGuardFn<URLSearchParams>;