Skip to content

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 Oct 6, 2022
1 parent 42c8d6e commit b1ae728
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 5 additions & 2 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 {
Empty,
GenericArguments,
GenericFunction,
MultiLevelObject,
PickEmptyType,
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 any>(data: T | Empty): data is PickEmptyType<T> {

try {
if (data === null || data === undefined || typeof data === 'undefined') {
return true;
Expand All @@ -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;
}

Expand Down
18 changes: 18 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,21 @@ export type TabsData = {
searchId: string;
[key:string]: unknown;
};


// isEmpty function

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

export type Empty =
| never[]
| Record<keyof any, never>
| ''
| null
| undefined
| never;

export type PickEmptyType<T> =
T extends AllowedValueType
? Empty
: T;

0 comments on commit b1ae728

Please sign in to comment.