@httpx/assert
Version:
Assertions and typeguards
42 lines (39 loc) • 1.37 kB
text/typescript
import { B as BasePlainObject, D as DefaultBasePlainObject, P as PlainObject } from './object.types-UWGd93VB.cjs';
import { M as MsgOrErrorFactory } from './internal.types-D5VRBw-5.cjs';
/**
* Assert a value is a plain object
*
* @example
* ```typescript
* import { assertPlainObject } from '@httpx/plain-object';
* import type { PlainObject } from '@httpx/plain-object';
*
* function fn(value: unknown) {
*
* // 👇 Throws `new TypeError('Not a plain object')` if not a plain object
* assertPlainObject(value);
*
* // 👇 Throws `new TypeError('Custom message')` if not a plain object
* assertPlainObject(value, 'Custom message');
*
* // 👇 Throws custom error if not a plain object
* assertPlainObject(value, () => {
* throw new HttpBadRequest('Custom message');
* });
*
* return value;
* }
*
* try {
* const value = fn({ key: 'value' });
* // ✅ Value is known to be PlainObject<unknown>
* assertType<PlainObject>(value);
* } catch (error) {
* console.error(error);
* }
* ```
*
* @throws TypeError
*/
declare function assertPlainObject<TValue extends BasePlainObject = DefaultBasePlainObject>(v: unknown, msgOrErrorFactory?: MsgOrErrorFactory): asserts v is TValue extends DefaultBasePlainObject ? BasePlainObject : PlainObject<TValue>;
export { assertPlainObject };