-
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(IsUnion): Add IsUnion type * test(IsUnion): Add IsUnion test * docs(IsUnion): Add IsUnion docs
- Loading branch information
1 parent
3893081
commit b0c212d
Showing
7 changed files
with
111 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,32 @@ | ||
# IsUnion\<T> | ||
|
||
## 개요 | ||
|
||
주어진 타입이 union 타입인지 여부를 판별하는 타입이에요. | ||
|
||
## 문법 | ||
|
||
```ts | ||
export type IsUnion<T, U = T> = | ||
IsNever<T> extends true | ||
? false | ||
: T extends any | ||
? IsEqual<T, U> extends true | ||
? false | ||
: true | ||
: false; | ||
``` | ||
|
||
- **T**: 검사할 타입이에요. | ||
- **U** - 비교를 위한 `T`의 복제 타입이에요. | ||
|
||
## 예제 | ||
|
||
```ts | ||
type T0 = IsUnion<[]>; // false | ||
type T1 = IsUnion<null>; // false | ||
type T2 = IsUnion<number>; // false | ||
|
||
type T3 = IsUnion<number | string>; // true | ||
type T4 = IsUnion<'foo' | 'bar'>; // true | ||
``` |
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,32 @@ | ||
# IsUnion\<T> | ||
|
||
## Overview | ||
|
||
A type that determines whether the given type is a union. | ||
|
||
## Syntax | ||
|
||
```ts | ||
export type IsUnion<T, U = T> = | ||
IsNever<T> extends true | ||
? false | ||
: T extends any | ||
? IsEqual<T, U> extends true | ||
? false | ||
: true | ||
: false; | ||
``` | ||
|
||
- **T**: The type to check. | ||
- **U** - A copy of the original type `T` used for comparison. | ||
|
||
## Examples | ||
|
||
```ts | ||
type T0 = IsUnion<[]>; // false | ||
type T1 = IsUnion<null>; // false | ||
type T2 = IsUnion<number>; // false | ||
|
||
type T3 = IsUnion<number | string>; // true | ||
type T4 = IsUnion<'foo' | 'bar'>; // true | ||
``` |
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 { IsNever } from './IsNever'; | ||
import { IsEqual } from './IsEqual'; | ||
|
||
/** | ||
* @description | ||
* A type that determines whether the given type is a union. | ||
* | ||
* @template T - The type to check. | ||
* @template U - A copy of the original type `T` used for comparison. | ||
* | ||
* @returns {Boolean} - `true` if the type is a union; otherwise, returns `false`. | ||
* | ||
* @example | ||
* IsUnion<[]>; // false | ||
* IsUnion<null>; // false | ||
* IsUnion<number>; // false | ||
* IsUnion<number | string>; // true | ||
* IsUnion<'foo' | 'bar'>; // true | ||
*/ | ||
export type IsUnion<T, U = T> = | ||
IsNever<T> extends true | ||
? false | ||
: T extends any | ||
? IsEqual<T, U> extends true | ||
? false | ||
: true | ||
: false; |
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,17 @@ | ||
import { IsUnion } from '@/predicate'; | ||
import { expectType } from 'tsd'; | ||
|
||
declare function isUnion<T>(): IsUnion<T>; | ||
|
||
// Should be `false` if the type is not union. | ||
expectType<false>(isUnion<[]>()); | ||
expectType<false>(isUnion<null>()); | ||
expectType<false>(isUnion<undefined>()); | ||
expectType<false>(isUnion<number>()); | ||
expectType<false>(isUnion<string>()); | ||
expectType<false>(isUnion<true>()); | ||
expectType<false>(isUnion<false>()); | ||
|
||
// Should be `true` if the type is union. | ||
expectType<true>(isUnion<number | string>()); | ||
expectType<true>(isUnion<'foo' | 'bar'>()); |