From b1ae728bee96f2a854b7d971a4025a058049f9e3 Mon Sep 17 00:00:00 2001 From: ayush Date: Thu, 6 Oct 2022 18:55:01 +0530 Subject: [PATCH] fix type narrowing issue in isEmpty function close #129 --- packages/ella/src/general/index.ts | 7 +++++-- packages/ella/src/utils/types/index.ts | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/ella/src/general/index.ts b/packages/ella/src/general/index.ts index 8324063b..852688a9 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 { + Empty, GenericArguments, GenericFunction, MultiLevelObject, + PickEmptyType, 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 | Empty): data is PickEmptyType { + try { if (data === null || data === undefined || typeof data === 'undefined') { return true; @@ -38,7 +41,7 @@ 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; } diff --git a/packages/ella/src/utils/types/index.ts b/packages/ella/src/utils/types/index.ts index e9f3ccb1..0b27d315 100644 --- a/packages/ella/src/utils/types/index.ts +++ b/packages/ella/src/utils/types/index.ts @@ -66,3 +66,21 @@ export type TabsData = { searchId: string; [key:string]: unknown; }; + + +// isEmpty function + +export type AllowedValueType = string | number | Array | object | null | undefined | Symbol; + +export type Empty = + | never[] + | Record + | '' + | null + | undefined + | never; + +export type PickEmptyType = + T extends AllowedValueType + ? Empty + : T;