@pivanov/utils
Version:
A focused collection of TypeScript utilities for modern web development
146 lines (145 loc) • 3.81 kB
TypeScript
/**
* Type guard for `boolean`.
*
* @example
* ```ts
* isBoolean(true); // true
* isBoolean('true'); // false
* ```
*/
export declare const isBoolean: (value: unknown) => value is boolean;
/**
* Type guard for `number` (including `NaN`).
*
* @example
* ```ts
* isNumber(42); // true
* isNumber('42'); // false
* ```
*/
export declare const isNumber: (value: unknown) => value is number;
/**
* Type guard for `string`.
*
* @example
* ```ts
* isString('hello'); // true
* ```
*/
export declare const isString: (value: unknown) => value is string;
/**
* Type guard for `function` (any callable).
*
* @example
* ```ts
* isFunction(() => {}); // true
* ```
*/
export declare const isFunction: (value: unknown) => value is CallableFunction;
/**
* Type guard for plain objects (literal `{}` or `Object.create(null)`).
* Rejects arrays, Dates, Maps, Sets, class instances, etc.
*
* @example
* ```ts
* isObject({}); // true
* isObject([]); // false
* isObject(new Date()); // false
* ```
*/
export declare const isObject: (value: unknown) => value is Record<string, unknown>;
/**
* Type guard for any non-null object (loose - accepts arrays, instances, etc.).
* Use this when you only need "is it an object reference?".
*
* @example
* ```ts
* isRecord([]); // true
* isRecord({}); // true
* isRecord(new Date()); // true
* isRecord(null); // false
* ```
*/
export declare const isRecord: (value: unknown) => value is Record<string, unknown>;
/**
* Type guard for `undefined`.
*/
export declare const isUndefined: (value: unknown) => value is undefined;
/**
* Type guard for `null`.
*/
export declare const isNull: (value: unknown) => value is null;
/**
* Type guard for `null` or `undefined`.
*
* @example
* ```ts
* isNil(null); // true
* isNil(undefined); // true
* isNil(0); // false
* ```
*/
export declare const isNil: (value: unknown) => value is null | undefined;
/**
* Type guard: value is not `undefined`. Useful as a filter predicate.
*
* @example
* ```ts
* [1, undefined, 2].filter(isDefined); // [1, 2] typed as number[]
* ```
*/
export declare const isDefined: <T>(value: T | undefined) => value is T;
/**
* Type guard for arrays. Thin wrapper over `Array.isArray` with a
* generic-friendly signature.
*/
export declare const isArray: <T = unknown>(value: unknown) => value is T[];
/**
* Type guard for `Date` instances.
*/
export declare const isDate: (value: unknown) => value is Date;
/**
* Type guard for `RegExp`.
*/
export declare const isRegExp: (value: unknown) => value is RegExp;
/**
* Type guard for `Error` (and subclasses).
*/
export declare const isError: (value: unknown) => value is Error;
/**
* Type guard for thenable / Promise-like values. Checks for a callable
* `then` property - aligns with the Promises/A+ spec.
*/
export declare const isPromise: <T = unknown>(value: unknown) => value is Promise<T>;
/**
* Type guard for `Map`.
*/
export declare const isMap: <K = unknown, V = unknown>(value: unknown) => value is Map<K, V>;
/**
* Type guard for `Set`.
*/
export declare const isSet: <T = unknown>(value: unknown) => value is Set<T>;
/**
* Type guard for JS primitives (string, number, boolean, bigint, symbol,
* null, undefined).
*/
export declare const isPrimitive: (value: unknown) => value is string | number | boolean | bigint | symbol | null | undefined;
/**
* Checks whether a collection or string is empty.
*
* - String: length === 0
* - Array: length === 0
* - Map/Set: size === 0
* - Plain object: no own enumerable string keys
* - null / undefined: true
*
* @example
* ```ts
* isEmpty([]); // true
* isEmpty({}); // true
* isEmpty(''); // true
* isEmpty(new Map()); // true
* isEmpty([1]); // false
* ```
*/
export declare const isEmpty: (value: unknown) => boolean;