From f67d09b28ed5f1b0185b1a72408f0a24734f9d82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chiciuc=20Nicu=C8=99or?= Date: Sat, 4 Apr 2020 07:32:56 +0300 Subject: [PATCH] fix: Extract type of the element of a tuple without indexing. (#22916) * Extract type of the element of a tuple without indexing. * Amend ElementOf to also support readonly arrays. --- components/_util/type.ts | 6 ++++++ components/transfer/renderListBody.tsx | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/components/_util/type.ts b/components/_util/type.ts index 7e3f82bd0ed8..7620c9db1cd4 100644 --- a/components/_util/type.ts +++ b/components/_util/type.ts @@ -3,3 +3,9 @@ export type Omit = Pick>; 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 E)[] ? E : never; diff --git a/components/transfer/renderListBody.tsx b/components/transfer/renderListBody.tsx index 792f4ec93364..3c065bff1f63 100644 --- a/components/transfer/renderListBody.tsx +++ b/components/transfer/renderListBody.tsx @@ -1,11 +1,11 @@ import * as React from 'react'; -import { Omit, tuple } from '../_util/type'; +import { ElementOf, Omit, tuple } from '../_util/type'; import { TransferItem } from '.'; import { TransferListProps, RenderedItem } from './list'; import ListItem from './ListItem'; export const OmitProps = tuple('handleFilter', 'handleClear', 'checkedKeys'); -export type OmitProp = typeof OmitProps[number]; +export type OmitProp = ElementOf; type PartialTransferListProps = Omit; export interface TransferListBodyProps extends PartialTransferListProps {