Skip to content

Commit

Permalink
feat(TupleToUnion): Add TupleToUnion (#67)
Browse files Browse the repository at this point in the history
* feat(TupleToUnion): Add TupleToUnion type

* feat(TupleToUnion): Add TupleToUnion test

* docs(TupleToUnion): Add TupleToUnion docs
  • Loading branch information
haejunejung authored Sep 4, 2024
1 parent 1bd2816 commit 453e4af
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/.vitepress/en.mts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ export default defineConfig({
text: 'StrictOmit',
link: '/reference/utilities/StrictOmit',
},
{
text: 'TupleToUnion',
link: '/reference/utilities/TupleToUnion',
},
],
},
],
Expand Down
4 changes: 4 additions & 0 deletions docs/.vitepress/ko.mts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ export default defineConfig({
text: 'StrictOmit',
link: '/ko/reference/utilities/StrictOmit',
},
{
text: 'TupleToUnion',
link: '/ko/reference/utilities/TupleToUnion',
},
],
},
],
Expand Down
21 changes: 21 additions & 0 deletions docs/ko/reference/utilities/TupleToUnion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# TupleToUnion\<T>

## Overview

배열 혹은 튜플을 유니온 타입으로 변환해요.

## 문법

```ts
type TupleToUnion<T> = T extends readonly unknown[] ? T[number] : never;
```

- **T**: 변환할 배열 혹은 튜플 타입이에요.

## 예제

```ts
type T0 = TupleToUnion<['a', 'b', 'c']>; // 'a' | 'b' | 'c'
type T1 = TupleToUnion<[1, 'a', true]>; // 1 | 'a' | true
type T2 = TupleToUnion<[{ key: 'value1' }, { key: 'value2' }]>; // { key: "value1";} | { key: "value2";}
```
21 changes: 21 additions & 0 deletions docs/reference/utilities/TupleToUnion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# TupleToUnion\<T>

## Overview

Convert a array/tuple into a union type of its elements.

## Syntax

```ts
type TupleToUnion<T> = T extends readonly unknown[] ? T[number] : never;
```

- **T**: The array/tuple type to convert.

## Example

```ts
type T0 = TupleToUnion<['a', 'b', 'c']>; // 'a' | 'b' | 'c'
type T1 = TupleToUnion<[1, 'a', true]>; // 1 | 'a' | true
type T2 = TupleToUnion<[{ key: 'value1' }, { key: 'value2' }]>; // { key: "value1";} | { key: "value2";}
```
13 changes: 13 additions & 0 deletions source/utilities/TupleToUnion.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @description - Convert a array/tuple into a union type of its elements.
*
* @template T - The array/tuple type to convert.
*
* @returns - The union type of T's elements.
*
* @example
* type T0 = TupleToUnion<['a', 'b', 'c']>; // 'a' | 'b' | 'c'
* type T1 = TupleToUnion<[1, 'a', true]>; // 1 | 'a' | true
* type T2 = TupleToUnion<[{ key: 'value1' }, { key: 'value2' }]>; // { key: "value1";} | { key: "value2";}
*/
export type TupleToUnion<T> = T extends readonly unknown[] ? T[number] : never;
1 change: 1 addition & 0 deletions source/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export type { Simplify } from './Simplify';
export type { StrictExclude } from './StrictExclude';
export type { StrictExtract } from './StrictExtract';
export type { StrictOmit } from './StrictOmit';
export type { TupleToUnion } from './TupleToUnion';
27 changes: 27 additions & 0 deletions test-d/utilities/TupleToUnion.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { TupleToUnion } from '@/utilities';
import { expectNotAssignable, expectType } from 'tsd';

declare function tupleToUnion<T>(): TupleToUnion<T>;

// Should be handle correctly tuple to union.
// It works correctly regardless of the order.
type T0 = ['a', 'b', 'c'];
type T1 = ['b', 'a', 'c'];
type T2 = ['c', 'b', 'a'];
expectType<'a' | 'b' | 'c'>(tupleToUnion<T0>());
expectType<'a' | 'b' | 'c'>(tupleToUnion<T1>());
expectType<'a' | 'b' | 'c'>(tupleToUnion<T2>());

// Should be handle correctly the object type.
type T3 = ['a' | { key: 'b' } | 'c'];
type T4 = [{ key: { k1: 'a' } }, { key: { k2: 'b' } }];
expectType<'a' | { key: 'b' } | 'c'>(tupleToUnion<T3>());
expectType<{ key: { k1: 'a' } } | { key: { k2: 'b' } }>(tupleToUnion<T4>());

// Should not be assingable except array/tuple type.
expectNotAssignable({});
expectNotAssignable(1);
expectNotAssignable('1');
expectNotAssignable(null);
expectNotAssignable(undefined);
expectNotAssignable(true);

0 comments on commit 453e4af

Please sign in to comment.