From 12543277b724791fbaa2fd5e64e25d8230ba1868 Mon Sep 17 00:00:00 2001 From: lijianan <574980606@qq.com> Date: Tue, 13 Dec 2022 14:57:40 +0800 Subject: [PATCH] =?UTF-8?q?type:=20remove=20tuple=E3=80=81tupleNum?= =?UTF-8?q?=E3=80=81ElementOf=20(#39478)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * type: remove tuple * type: remove tuple * type: remove tuple * fix * fix * fix type * fix type * fix type * fix type * fix type * Update ColorPicker.tsx * Update components/badge/utils.tsx Co-authored-by: MadCcc <1075746765@qq.com> * fix type * fix type * fix type * fix * fix * fix type * fix type * fix type * update snap * update snap Co-authored-by: MadCcc <1075746765@qq.com> --- components/_util/colors.ts | 20 +++++++++++-------- components/_util/motion.tsx | 4 ++-- components/_util/statusUtils.tsx | 4 ++-- components/_util/type.ts | 11 ---------- components/badge/utils.tsx | 2 +- components/button/button.tsx | 10 +++++----- .../date-picker/generatePicker/index.tsx | 3 +-- components/drawer/index.tsx | 3 +-- components/dropdown/dropdown.tsx | 5 ++--- components/form/FormItem/index.tsx | 3 +-- components/grid/row.tsx | 12 ++++++++--- components/input/ClearableLabeledInput.tsx | 3 +-- components/progress/progress.tsx | 5 ++--- components/spin/index.tsx | 4 +--- components/table/interface.tsx | 4 +--- components/tag/index.tsx | 4 +--- components/tooltip/index.tsx | 1 - components/transfer/ListBody.tsx | 6 ++---- components/typography/Title.tsx | 3 +-- 19 files changed, 45 insertions(+), 62 deletions(-) diff --git a/components/_util/colors.ts b/components/_util/colors.ts index da8a36a681..8a3a6714ac 100644 --- a/components/_util/colors.ts +++ b/components/_util/colors.ts @@ -1,9 +1,12 @@ -import type { ElementOf } from './type'; -import { tuple } from './type'; +export const PresetStatusColorTypes = [ + 'success', + 'processing', + 'error', + 'default', + 'warning', +] as const; -export const PresetStatusColorTypes = tuple('success', 'processing', 'error', 'default', 'warning'); -// eslint-disable-next-line import/prefer-default-export -export const PresetColorTypes = tuple( +export const PresetColorTypes = [ 'pink', 'red', 'yellow', @@ -17,7 +20,8 @@ export const PresetColorTypes = tuple( 'volcano', 'gold', 'lime', -); +] as const; -export type PresetColorType = ElementOf; -export type PresetStatusColorType = ElementOf; +export type PresetColorType = typeof PresetColorTypes[number]; + +export type PresetStatusColorType = typeof PresetStatusColorTypes[number]; diff --git a/components/_util/motion.tsx b/components/_util/motion.tsx index f8cc1352a2..67d2b52fc7 100644 --- a/components/_util/motion.tsx +++ b/components/_util/motion.tsx @@ -1,6 +1,5 @@ import type { CSSMotionProps, MotionEndEventHandler, MotionEventHandler } from 'rc-motion'; import type { MotionEvent } from 'rc-motion/lib/interface'; -import { tuple } from './type'; // ================== Collapse Motion ================== const getCollapsedHeight: MotionEventHandler = () => ({ height: 0, opacity: 0 }); @@ -26,7 +25,8 @@ const initCollapseMotion = (rootCls: string = 'ant'): CSSMotionProps => ({ motionDeadline: 500, }); -const SelectPlacements = tuple('bottomLeft', 'bottomRight', 'topLeft', 'topRight'); +const SelectPlacements = ['bottomLeft', 'bottomRight', 'topLeft', 'topRight'] as const; + export type SelectCommonPlacement = typeof SelectPlacements[number]; const getTransitionDirection = (placement: SelectCommonPlacement | undefined) => { diff --git a/components/_util/statusUtils.tsx b/components/_util/statusUtils.tsx index b6a6d42577..08358b6ab2 100644 --- a/components/_util/statusUtils.tsx +++ b/components/_util/statusUtils.tsx @@ -1,8 +1,8 @@ import classNames from 'classnames'; import type { ValidateStatus } from '../form/FormItem'; -import { tuple } from './type'; -const InputStatuses = tuple('warning', 'error', ''); +const InputStatuses = ['warning', 'error', ''] as const; + export type InputStatus = typeof InputStatuses[number]; export function getStatusClassNames( diff --git a/components/_util/type.ts b/components/_util/type.ts index a42c2b8fb9..1e00fe20c5 100644 --- a/components/_util/type.ts +++ b/components/_util/type.ts @@ -1,13 +1,2 @@ -// https://stackoverflow.com/questions/46176165/ways-to-get-string-literal-type-of-array-values-without-enum-overhead -export const tuple = (...args: T) => args; - -export const tupleNum = (...args: T) => args; - -/** - * https://stackoverflow.com/a/59187769 Extract the type of an element of an array/tuple without - * performing indexing - */ -export type ElementOf = T extends (infer E)[] ? E : T extends readonly (infer F)[] ? F : never; - /** https://github.com/Microsoft/TypeScript/issues/29729 */ export type LiteralUnion = T | (U & {}); diff --git a/components/badge/utils.tsx b/components/badge/utils.tsx index 2c9a500e88..bb495314e7 100644 --- a/components/badge/utils.tsx +++ b/components/badge/utils.tsx @@ -2,5 +2,5 @@ import { PresetColorTypes } from '../_util/colors'; // eslint-disable-next-line import/prefer-default-export export function isPresetColor(color?: string): boolean { - return (PresetColorTypes as any[]).includes(color); + return PresetColorTypes.includes(color as any); } diff --git a/components/button/button.tsx b/components/button/button.tsx index c8936c87d4..53ddf70ab2 100644 --- a/components/button/button.tsx +++ b/components/button/button.tsx @@ -2,14 +2,12 @@ import classNames from 'classnames'; import omit from 'rc-util/lib/omit'; import * as React from 'react'; - import { ConfigContext } from '../config-provider'; import DisabledContext from '../config-provider/DisabledContext'; import type { SizeType } from '../config-provider/SizeContext'; import SizeContext from '../config-provider/SizeContext'; import { useCompactItemContext } from '../space/Compact'; import { cloneElement, isFragment } from '../_util/reactNode'; -import { tuple } from '../_util/type'; import warning from '../_util/warning'; import Wave from '../_util/wave'; import Group, { GroupSizeContext } from './button-group'; @@ -78,11 +76,13 @@ function spaceChildren(children: React.ReactNode, needInserted: boolean) { ); } -const ButtonTypes = tuple('default', 'primary', 'ghost', 'dashed', 'link', 'text'); +const ButtonTypes = ['default', 'primary', 'ghost', 'dashed', 'link', 'text'] as const; export type ButtonType = typeof ButtonTypes[number]; -const ButtonShapes = tuple('default', 'circle', 'round'); + +const ButtonShapes = ['default', 'circle', 'round'] as const; export type ButtonShape = typeof ButtonShapes[number]; -const ButtonHTMLTypes = tuple('submit', 'button', 'reset'); + +const ButtonHTMLTypes = ['submit', 'button', 'reset'] as const; export type ButtonHTMLType = typeof ButtonHTMLTypes[number]; export type LegacyButtonType = ButtonType | 'danger'; diff --git a/components/date-picker/generatePicker/index.tsx b/components/date-picker/generatePicker/index.tsx index a9d453bc41..0df45094eb 100644 --- a/components/date-picker/generatePicker/index.tsx +++ b/components/date-picker/generatePicker/index.tsx @@ -14,7 +14,6 @@ import type { import type { SizeType } from '../../config-provider/SizeContext'; import type { TimePickerLocale } from '../../time-picker'; import type { InputStatus } from '../../_util/statusUtils'; -import { tuple } from '../../_util/type'; import PickerButton from '../PickerButton'; import generateRangePicker from './generateRangePicker'; import generateSinglePicker from './generateSinglePicker'; @@ -70,7 +69,7 @@ export function getTimeProps( showTime: showTimeObj, }; } -const DataPickerPlacements = tuple('bottomLeft', 'bottomRight', 'topLeft', 'topRight'); +const DataPickerPlacements = ['bottomLeft', 'bottomRight', 'topLeft', 'topRight'] as const; type DataPickerPlacement = typeof DataPickerPlacements[number]; type InjectDefaultProps = Omit< diff --git a/components/drawer/index.tsx b/components/drawer/index.tsx index fbc89e87d1..8c19d95cfa 100644 --- a/components/drawer/index.tsx +++ b/components/drawer/index.tsx @@ -6,7 +6,6 @@ import * as React from 'react'; import { ConfigContext } from '../config-provider'; import { NoFormStyle } from '../form/context'; import { getTransitionName } from '../_util/motion'; -import { tuple } from '../_util/type'; import warning from '../_util/warning'; import DrawerPanel from './DrawerPanel'; import type { DrawerPanelProps } from './DrawerPanel'; @@ -15,7 +14,7 @@ import type { DrawerPanelProps } from './DrawerPanel'; import useStyle from './style'; import { NoCompactStyle } from '../space/Compact'; -const SizeTypes = tuple('default', 'large'); +const SizeTypes = ['default', 'large'] as const; type sizeType = typeof SizeTypes[number]; export interface PushState { diff --git a/components/dropdown/dropdown.tsx b/components/dropdown/dropdown.tsx index d424dddc94..7c9486aedc 100644 --- a/components/dropdown/dropdown.tsx +++ b/components/dropdown/dropdown.tsx @@ -11,13 +11,12 @@ import { OverrideProvider } from '../menu/OverrideContext'; import genPurePanel from '../_util/PurePanel'; import getPlacements from '../_util/placements'; import { cloneElement } from '../_util/reactNode'; -import { tuple } from '../_util/type'; import warning from '../_util/warning'; import { NoCompactStyle } from '../space/Compact'; import DropdownButton from './dropdown-button'; import useStyle from './style'; -const Placements = tuple( +const Placements = [ 'topLeft', 'topCenter', 'topRight', @@ -26,7 +25,7 @@ const Placements = tuple( 'bottomRight', 'top', 'bottom', -); +] as const; type Placement = typeof Placements[number]; diff --git a/components/form/FormItem/index.tsx b/components/form/FormItem/index.tsx index 21bd7061fc..292583628a 100644 --- a/components/form/FormItem/index.tsx +++ b/components/form/FormItem/index.tsx @@ -9,7 +9,6 @@ import * as React from 'react'; import useFormItemStatus from '../hooks/useFormItemStatus'; import { ConfigContext } from '../../config-provider'; import { cloneElement, isValidElement } from '../../_util/reactNode'; -import { tuple } from '../../_util/type'; import warning from '../../_util/warning'; import { FormContext, NoStyleItemContext } from '../context'; import type { FormItemInputProps } from '../FormItemInput'; @@ -28,7 +27,7 @@ interface FieldError { warnings: string[]; } -const ValidateStatuses = tuple('success', 'warning', 'error', 'validating', ''); +const ValidateStatuses = ['success', 'warning', 'error', 'validating', ''] as const; export type ValidateStatus = typeof ValidateStatuses[number]; type RenderChildren = (form: FormInstance) => React.ReactNode; diff --git a/components/grid/row.tsx b/components/grid/row.tsx index fc458815d8..96bfc87f4c 100644 --- a/components/grid/row.tsx +++ b/components/grid/row.tsx @@ -4,12 +4,18 @@ import { ConfigContext } from '../config-provider'; import useFlexGapSupport from '../_util/hooks/useFlexGapSupport'; import type { Breakpoint, ScreenMap } from '../_util/responsiveObserve'; import ResponsiveObserve, { responsiveArray } from '../_util/responsiveObserve'; -import { tuple } from '../_util/type'; import RowContext from './RowContext'; import { useRowStyle } from './style'; -const RowAligns = tuple('top', 'middle', 'bottom', 'stretch'); -const RowJustify = tuple('start', 'end', 'center', 'space-around', 'space-between', 'space-evenly'); +const RowAligns = ['top', 'middle', 'bottom', 'stretch'] as const; +const RowJustify = [ + 'start', + 'end', + 'center', + 'space-around', + 'space-between', + 'space-evenly', +] as const; type Responsive = 'xxl' | 'xl' | 'lg' | 'md' | 'sm' | 'xs'; type ResponsiveLike = { diff --git a/components/input/ClearableLabeledInput.tsx b/components/input/ClearableLabeledInput.tsx index cd58050a18..a809850c56 100644 --- a/components/input/ClearableLabeledInput.tsx +++ b/components/input/ClearableLabeledInput.tsx @@ -8,10 +8,9 @@ import { FormItemInputContext } from '../form/context'; import { cloneElement } from '../_util/reactNode'; import type { InputStatus } from '../_util/statusUtils'; import { getMergedStatus, getStatusClassNames } from '../_util/statusUtils'; -import { tuple } from '../_util/type'; import type { InputProps } from './Input'; -const ClearableInputType = tuple('text', 'input'); +const ClearableInputType = ['text', 'input'] as const; function hasAddon(props: InputProps | ClearableInputProps) { return !!(props.addonBefore || props.addonAfter); diff --git a/components/progress/progress.tsx b/components/progress/progress.tsx index 421c3caec0..fedc37b380 100644 --- a/components/progress/progress.tsx +++ b/components/progress/progress.tsx @@ -7,7 +7,6 @@ import omit from 'rc-util/lib/omit'; import * as React from 'react'; import type { ConfigConsumerProps } from '../config-provider'; import { ConfigContext } from '../config-provider'; -import { tuple } from '../_util/type'; import warning from '../_util/warning'; import Circle from './Circle'; import Line from './Line'; @@ -15,9 +14,9 @@ import Steps from './Steps'; import { getSuccessPercent, validProgress } from './utils'; import useStyle from './style'; -const ProgressTypes = tuple('line', 'circle', 'dashboard'); +const ProgressTypes = ['line', 'circle', 'dashboard'] as const; export type ProgressType = typeof ProgressTypes[number]; -const ProgressStatuses = tuple('normal', 'exception', 'active', 'success'); +const ProgressStatuses = ['normal', 'exception', 'active', 'success'] as const; export type ProgressSize = 'default' | 'small'; export type StringGradients = { [percentage: string]: string }; type FromToGradients = { from: string; to: string }; diff --git a/components/spin/index.tsx b/components/spin/index.tsx index 463d2bdb8b..a5ebc3d717 100644 --- a/components/spin/index.tsx +++ b/components/spin/index.tsx @@ -5,11 +5,9 @@ import * as React from 'react'; import type { ConfigConsumerProps } from '../config-provider'; import { ConfigConsumer, ConfigContext } from '../config-provider'; import { cloneElement, isValidElement } from '../_util/reactNode'; -import { tuple } from '../_util/type'; - import useStyle from './style/index'; -const SpinSizes = tuple('small', 'default', 'large'); +const SpinSizes = ['small', 'default', 'large'] as const; export type SpinSize = typeof SpinSizes[number]; export type SpinIndicator = React.ReactElement; diff --git a/components/table/interface.tsx b/components/table/interface.tsx index b09d80d858..7d113012e0 100644 --- a/components/table/interface.tsx +++ b/components/table/interface.tsx @@ -9,9 +9,7 @@ import type { CheckboxProps } from '../checkbox'; import type { PaginationProps } from '../pagination'; import type { TooltipProps } from '../tooltip'; import type { Breakpoint } from '../_util/responsiveObserve'; -import { tuple } from '../_util/type'; import type { INTERNAL_SELECTION_ITEM } from './hooks/useSelection'; -// import { TableAction } from './Table'; export { GetRowKey, ExpandableConfig }; @@ -45,7 +43,7 @@ export interface TableLocale { export type SortOrder = 'descend' | 'ascend' | null; -const TableActions = tuple('paginate', 'sort', 'filter'); +const TableActions = ['paginate', 'sort', 'filter'] as const; export type TableAction = typeof TableActions[number]; export type CompareFn = (a: T, b: T, sortOrder?: SortOrder) => number; diff --git a/components/tag/index.tsx b/components/tag/index.tsx index fef00deeed..867c0ddb09 100644 --- a/components/tag/index.tsx +++ b/components/tag/index.tsx @@ -1,16 +1,14 @@ import CloseOutlined from '@ant-design/icons/CloseOutlined'; import classNames from 'classnames'; import * as React from 'react'; - import { ConfigContext } from '../config-provider'; import type { PresetColorType, PresetStatusColorType } from '../_util/colors'; import { PresetColorTypes, PresetStatusColorTypes } from '../_util/colors'; -import type { LiteralUnion } from '../_util/type'; import Wave from '../_util/wave'; import warning from '../_util/warning'; import CheckableTag from './CheckableTag'; - import useStyle from './style'; +import type { LiteralUnion } from '../_util/type'; export type { CheckableTagProps } from './CheckableTag'; diff --git a/components/tooltip/index.tsx b/components/tooltip/index.tsx index 8d07d4f41a..e3c134806c 100644 --- a/components/tooltip/index.tsx +++ b/components/tooltip/index.tsx @@ -14,7 +14,6 @@ import { cloneElement, isValidElement, isFragment } from '../_util/reactNode'; import type { LiteralUnion } from '../_util/type'; import warning from '../_util/warning'; import PurePanel from './PurePanel'; - import useStyle from './style'; import { parseColor } from './util'; diff --git a/components/transfer/ListBody.tsx b/components/transfer/ListBody.tsx index e5f57703c3..f45150c5db 100644 --- a/components/transfer/ListBody.tsx +++ b/components/transfer/ListBody.tsx @@ -2,14 +2,12 @@ import classNames from 'classnames'; import * as React from 'react'; import type { KeyWiseTransferItem } from '.'; import Pagination from '../pagination'; -import type { ElementOf } from '../_util/type'; -import { tuple } from '../_util/type'; import type { PaginationType } from './interface'; import type { RenderedItem, TransferListProps } from './list'; import ListItem from './ListItem'; -export const OmitProps = tuple('handleFilter', 'handleClear', 'checkedKeys'); -export type OmitProp = ElementOf; +export const OmitProps = ['handleFilter', 'handleClear', 'checkedKeys'] as const; +export type OmitProp = typeof OmitProps[number]; type PartialTransferListProps = Omit, OmitProp>; export interface TransferListBodyProps extends PartialTransferListProps { diff --git a/components/typography/Title.tsx b/components/typography/Title.tsx index 1cd1f84246..926d9f6500 100644 --- a/components/typography/Title.tsx +++ b/components/typography/Title.tsx @@ -1,10 +1,9 @@ import * as React from 'react'; -import { tupleNum } from '../_util/type'; import warning from '../_util/warning'; import type { BlockProps } from './Base'; import Base from './Base'; -const TITLE_ELE_LIST = tupleNum(1, 2, 3, 4, 5); +const TITLE_ELE_LIST = [1, 2, 3, 4, 5] as const; export interface TitleProps extends Omit, 'strong'>,