-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(TupleToUnion): Add
TupleToUnion
(#67)
* feat(TupleToUnion): Add TupleToUnion type * feat(TupleToUnion): Add TupleToUnion test * docs(TupleToUnion): Add TupleToUnion docs
- Loading branch information
1 parent
1bd2816
commit 453e4af
Showing
7 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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";} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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";} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |