Skip to content

Commit

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

* test(IsUnion): Add IsUnion test

* docs(IsUnion): Add IsUnion docs
  • Loading branch information
haejunejung authored Sep 24, 2024
1 parent 3893081 commit b0c212d
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/en.mts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default defineConfig({
{ text: 'IsEqual', link: '/reference/predicate/IsEqual' },
{ text: 'IsTuple', link: '/reference/predicate/IsTuple' },
{ text: 'IsNever', link: '/reference/predicate/IsNever' },
{ text: 'IsUnion', link: '/reference/predicate/IsUnion' },
{ text: 'IsUnknown', link: '/reference/predicate/IsUnknown' },
],
},
Expand Down
1 change: 1 addition & 0 deletions docs/.vitepress/ko.mts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export default defineConfig({
{ text: 'IsEqual', link: '/ko/reference/predicate/IsEqual' },
{ text: 'IsTuple', link: '/ko/reference/predicate/IsTuple' },
{ text: 'IsNever', link: '/ko/reference/predicate/IsNever' },
{ text: 'IsUnion', link: '/ko/reference/predicate/IsUnion' },
{ text: 'IsUnknown', link: '/ko/reference/predicate/IsUnknown' },
],
},
Expand Down
32 changes: 32 additions & 0 deletions docs/ko/reference/predicate/IsUnion.md
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
```
32 changes: 32 additions & 0 deletions docs/reference/predicate/IsUnion.md
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
```
27 changes: 27 additions & 0 deletions source/predicate/IsUnion.ts
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;
1 change: 1 addition & 0 deletions source/predicate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export type { IsArray } from './IsArray';
export type { IsEqual } from './IsEqual';
export type { IsTuple } from './IsTuple';
export type { IsNever } from './IsNever';
export type { IsUnion } from './IsUnion';
export type { IsUnknown } from './IsUnknown';
17 changes: 17 additions & 0 deletions test-d/predicate/IsUnion.test-d.ts
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'>());

0 comments on commit b0c212d

Please sign in to comment.