Skip to content

Commit

Permalink
feat: Make isEmpty a type guard (silvermine#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
crgwbr committed Jul 23, 2024
1 parent 4cf6f28 commit 83932c8
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 @@ -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<never> |
IEmptyObj
);

/**
* Checks if `o` is an empty object. An object is "empty" if it:
*
Expand All @@ -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;
}
Expand Down

0 comments on commit 83932c8

Please sign in to comment.