Skip to content

Commit

Permalink
feat: Make isEmpty a type guard (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
crgwbr committed Jul 23, 2024
1 parent 7d694b1 commit 9724e4d
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/utils/is-empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ import { isNumber } from './is-number';
import { isSet } from './is-set';
import { isObject } from './is-object';


interface IEmptyArguments extends IArguments {
length: 0;
}

interface IEmptyObj {
[s: string]: never;
}

type IEmptyTypes = (
null |
undefined |
boolean |
number |
never[] |
'' |
IEmptyArguments |
Set<never> |
IEmptyObj
);

/**
* Checks if `o` is an empty object. An object is "empty" if it:
*
Expand All @@ -17,7 +38,7 @@ import { isObject } from './is-object';
*
* @returns `true` if `o` is empty
*/
export function isEmpty(o: unknown): boolean {
export function isEmpty(o: unknown): o is IEmptyTypes {
if (isNull(o) || isUndefined(o) || isBoolean(o) || isNumber(o)) {
return true;
}
Expand Down

0 comments on commit 9724e4d

Please sign in to comment.