From 83932c87fb1a214fa97af99b807e1ad487774758 Mon Sep 17 00:00:00 2001 From: Craig Weber Date: Tue, 23 Jul 2024 10:48:57 -0400 Subject: [PATCH] feat: Make isEmpty a type guard (#16) --- src/utils/is-empty.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/utils/is-empty.ts b/src/utils/is-empty.ts index 1c5ed36..0ea8fed 100644 --- a/src/utils/is-empty.ts +++ b/src/utils/is-empty.ts @@ -4,6 +4,27 @@ import { isArguments } from './is-arguments'; import { isUndefined } from './is-undefined'; import { isSet } from './is-set'; + +interface IEmptyArguments extends IArguments { + length: 0; +} + +interface IEmptyObj { + [s: string]: never; +} + +type IEmptyTypes = ( + null | + undefined | + boolean | + number | + never[] | + '' | + IEmptyArguments | + Set | + IEmptyObj +); + /** * Checks if `o` is an empty object. An object is "empty" if it: * @@ -13,7 +34,7 @@ import { isSet } from './is-set'; * * @returns `true` if `o` is empty */ -export function isEmpty(o: any): boolean { +export function isEmpty(o: any): o is IEmptyTypes { if (o === null || isUndefined(o)) { return true; }