Skip to content

Commit 8303936

Browse files
authored
refactor(utils): refactor shared type guard utilities (ant-design#57777)
1 parent b70ce24 commit 8303936

6 files changed

Lines changed: 18 additions & 23 deletions

File tree

.dumi/hooks/useLocalStorage.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import React, { useCallback, useEffect } from 'react';
22
import { useEvent } from '@rc-component/util';
3+
import { isFunction } from 'antd/lib/_util/is';
34

45
const ANT_SYNC_STORAGE_EVENT_KEY = 'ANT_SYNC_STORAGE_EVENT_KEY';
56

6-
const isFunction = (val: any): val is (...args: any[]) => any => {
7-
return typeof val === 'function';
8-
};
9-
107
interface Options<T> {
118
defaultValue?: T;
129
serializer?: (value: T) => string;

components/_util/ActionButton.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import useState from '@rc-component/util/lib/hooks/useState';
44
import Button from '../button/Button';
55
import type { ButtonProps, LegacyButtonType } from '../button/Button';
66
import { convertLegacyProps } from '../button/buttonHelpers';
7+
import { isThenable } from './is';
78

89
export interface ActionButtonProps {
910
type?: LegacyButtonType;
@@ -22,10 +23,6 @@ export interface ActionButtonProps {
2223
isSilent?: () => boolean;
2324
}
2425

25-
const isThenable = <T,>(thing?: PromiseLike<T>): thing is PromiseLike<T> => {
26-
return typeof thing?.then === 'function';
27-
};
28-
2926
const ActionButton: React.FC<ActionButtonProps> = (props) => {
3027
const {
3128
type,

components/_util/is.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// \b([A-Za-z_$][\w$]*)\s*!==\s*(?:undefined\s*&&\s*\1\s*!==\s*null|null\s*&&\s*\1\s*!==\s*undefined)\b
2-
// \b([A-Za-z_$][\w$\.]*)\s*===\s*(?:undefined|null)\s*\|\|\s*\1\s*===\s*(?:undefined|null)\b
31
export const isNonNullable = <T>(val: T): val is NonNullable<T> => {
42
return val !== undefined && val !== null;
53
};
@@ -8,6 +6,18 @@ export const isNumber = (val: any): val is number => {
86
return typeof val === 'number' && !Number.isNaN(val);
97
};
108

9+
export const isString = (val: any): val is string => {
10+
return typeof val === 'string';
11+
};
12+
1113
export const isPrimitive = (value: any) => {
1214
return (typeof value !== 'object' && typeof value !== 'function') || value === null;
1315
};
16+
17+
export const isFunction = (val: any): val is (...args: any[]) => any => {
18+
return typeof val === 'function';
19+
};
20+
21+
export const isThenable = <T>(val?: PromiseLike<T>): val is PromiseLike<T> => {
22+
return isNonNullable(val) && isFunction(val.then);
23+
};

components/_util/reactNode.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22

3+
import { isFunction } from './is';
34
import type { AnyObject } from './type';
45

56
export function isFragment(child: any): boolean {
@@ -16,10 +17,7 @@ export const replaceElement = <P>(
1617
if (!React.isValidElement<P>(element)) {
1718
return replacement;
1819
}
19-
return React.cloneElement<P>(
20-
element,
21-
typeof props === 'function' ? props(element.props || {}) : props,
22-
);
20+
return React.cloneElement<P>(element, isFunction(props) ? props(element.props || {}) : props);
2321
};
2422

2523
export function cloneElement<P>(element: React.ReactNode, props?: RenderProps) {

components/button/buttonHelpers.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { clsx } from 'clsx';
33

4-
import { isNonNullable } from '../_util/is';
4+
import { isNonNullable, isString } from '../_util/is';
55
import { cloneElement, isFragment } from '../_util/reactNode';
66
import { PresetColors } from '../theme/interface';
77
import type { BaseButtonProps, LegacyButtonType } from './Button';
@@ -19,10 +19,6 @@ export function convertLegacyProps(
1919
return { type };
2020
}
2121

22-
export function isString(str: unknown): str is string {
23-
return typeof str === 'string';
24-
}
25-
2622
export function isUnBorderedButtonVariant(type?: ButtonVariantType) {
2723
return type === 'text' || type === 'link';
2824
}

components/dropdown/dropdown.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,7 @@ const Dropdown: CompoundedComponent = (props) => {
224224

225225
const child = React.Children.only(
226226
isPrimitive(children) ? <span>{children}</span> : children,
227-
) as React.ReactElement<{
228-
className?: string;
229-
disabled?: boolean;
230-
}>;
227+
) as React.ReactElement<{ className?: string; disabled?: boolean }>;
231228

232229
const popupTrigger = cloneElement(child, {
233230
className: clsx(

0 commit comments

Comments
 (0)