Skip to content

Commit

Permalink
fix: Extract type of the element of a tuple without indexing. (ant-de…
Browse files Browse the repository at this point in the history
…sign#22916)

* Extract type of the element of a tuple without indexing.

* Amend ElementOf<T> to also support readonly arrays.
  • Loading branch information
nicu-chiciuc authored Apr 4, 2020
1 parent b317b7c commit f67d09b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions components/_util/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
export const tuple = <T extends string[]>(...args: T) => args;

export const tupleNum = <T extends number[]>(...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> = T extends (infer E)[] ? E : T extends readonly (infer E)[] ? E : never;
4 changes: 2 additions & 2 deletions components/transfer/renderListBody.tsx
Original file line number Diff line number Diff line change
@@ -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<typeof OmitProps>;
type PartialTransferListProps = Omit<TransferListProps, OmitProp>;

export interface TransferListBodyProps extends PartialTransferListProps {
Expand Down

0 comments on commit f67d09b

Please sign in to comment.