Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
fix: type narrowing issue in isEmpty function
Browse files Browse the repository at this point in the history
Close #129
  • Loading branch information
ayush-mani-tripathi committed Sep 29, 2022
1 parent 42c8d6e commit e60ebee
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/ella/src/general/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import { dispatchCustomEvent } from '../dom';
import { CUSTOM_EVENTS } from '../utils/constants';
import {
AllowedValueType,
GenericArguments,
GenericFunction,
MultiLevelObject,
PickAllowedValueType,
SingleLevelObject,
TabsData
} from '../utils/types';
Expand All @@ -27,7 +29,8 @@ export { default as isEqual } from 'lodash.isequal';
* }
* ```
*/
export function isEmpty(data: any) {
export function isEmpty<T extends AllowedValueType>(data: T | AllowedValueType): data is PickAllowedValueType<T> {

try {
if (data === null || data === undefined || typeof data === 'undefined') {
return true;
Expand All @@ -38,14 +41,14 @@ export function isEmpty(data: any) {
switch (dataType) {

case 'string':
if (data.trim() === '' || data === 'null' || data === null) {
if ((data as string).trim() === '' || (data as string) === 'null' || data === null) {
return true;
}

return false;

case 'object':
const keys = Object.keys(data);
const keys = Object.keys(data as object);
const len = keys.length;

if (len <= 0) {
Expand Down
7 changes: 7 additions & 0 deletions packages/ella/src/utils/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,10 @@ export type TabsData = {
searchId: string;
[key:string]: unknown;
};

// isEmpty type
export type AllowedValueType = string | number | Array<any> | object | null | undefined | Symbol;

export type PickAllowedValueType<T> =
T extends AllowedValueType
? T : never;

0 comments on commit e60ebee

Please sign in to comment.