From e60ebee2c60f4b95baaebab209235601892eb722 Mon Sep 17 00:00:00 2001 From: ayush Date: Fri, 30 Sep 2022 00:35:44 +0530 Subject: [PATCH] fix: type narrowing issue in isEmpty function Close #129 --- packages/ella/src/general/index.ts | 9 ++++++--- packages/ella/src/utils/types/index.ts | 7 +++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/ella/src/general/index.ts b/packages/ella/src/general/index.ts index 8324063b..498f4eb2 100644 --- a/packages/ella/src/general/index.ts +++ b/packages/ella/src/general/index.ts @@ -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'; @@ -27,7 +29,8 @@ export { default as isEqual } from 'lodash.isequal'; * } * ``` */ -export function isEmpty(data: any) { + export function isEmpty(data: T | AllowedValueType): data is PickAllowedValueType { + try { if (data === null || data === undefined || typeof data === 'undefined') { return true; @@ -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) { diff --git a/packages/ella/src/utils/types/index.ts b/packages/ella/src/utils/types/index.ts index e9f3ccb1..c58fd437 100644 --- a/packages/ella/src/utils/types/index.ts +++ b/packages/ella/src/utils/types/index.ts @@ -66,3 +66,10 @@ export type TabsData = { searchId: string; [key:string]: unknown; }; + +// isEmpty type +export type AllowedValueType = string | number | Array | object | null | undefined | Symbol; + +export type PickAllowedValueType = + T extends AllowedValueType + ? T : never; \ No newline at end of file