Skip to content

Commit b3d92ed

Browse files
authored
Expose IsUnion type (#1137)
1 parent 474d904 commit b3d92ed

File tree

7 files changed

+43
-38
lines changed

7 files changed

+43
-38
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ export type {Or} from './source/or.d.ts';
144144
export type {NonEmptyTuple} from './source/non-empty-tuple.d.ts';
145145
export type {FindGlobalInstanceType, FindGlobalType} from './source/find-global-type.d.ts';
146146
export type {If} from './source/if.d.ts';
147+
export type {IsUnion} from './source/is-union.d.ts';
147148

148149
// Template literal types
149150
export type {CamelCase} from './source/camel-case.d.ts';

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ Click the type names for complete docs.
223223
- [`IsEmptyObject`](source/empty-object.d.ts) - Returns a boolean for whether the type is strictly equal to an empty plain object, the `{}` value.
224224
- [`IsNull`](source/is-null.d.ts) - Returns a boolean for whether the given type is `null`.
225225
- [`IsTuple`](source/is-tuple.d.ts) - Returns a boolean for whether the given array is a tuple.
226+
- [`IsUnion`](source/is-union.d.ts) - Returns a boolean for whether the given type is a union.
226227

227228
### JSON
228229

source/internal/type.d.ts

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -79,41 +79,6 @@ export type Not<A extends boolean> = A extends true
7979
? true
8080
: never;
8181

82-
/**
83-
Returns a boolean for whether the given type is a union type.
84-
85-
@example
86-
```
87-
type A = IsUnion<string | number>;
88-
//=> true
89-
90-
type B = IsUnion<string>;
91-
//=> false
92-
```
93-
*/
94-
export type IsUnion<T> = InternalIsUnion<T>;
95-
96-
/**
97-
The actual implementation of `IsUnion`.
98-
*/
99-
type InternalIsUnion<T, U = T> =
100-
(
101-
// @link https://ghaiklor.github.io/type-challenges-solutions/en/medium-isunion.html
102-
IsNever<T> extends true
103-
? false
104-
: T extends any
105-
? [U] extends [T]
106-
? false
107-
: true
108-
: never
109-
) extends infer Result
110-
// In some cases `Result` will return `false | true` which is `boolean`,
111-
// that means `T` has at least two types and it's a union type,
112-
// so we will return `true` instead of `boolean`.
113-
? boolean extends Result ? true
114-
: Result
115-
: never; // Should never happen
116-
11782
/**
11883
An if-else-like type that resolves depending on whether the given type is `any` or `never`.
11984

source/is-union.d.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type {IsNever} from './is-never.d.ts';
2+
3+
/**
4+
Returns a boolean for whether the given type is a union.
5+
6+
@example
7+
```
8+
import type {IsUnion} from 'type-fest';
9+
10+
type A = IsUnion<string | number>;
11+
//=> true
12+
13+
type B = IsUnion<string>;
14+
//=> false
15+
```
16+
*/
17+
export type IsUnion<T> = InternalIsUnion<T>;
18+
19+
/**
20+
The actual implementation of `IsUnion`.
21+
*/
22+
type InternalIsUnion<T, U = T> =
23+
(
24+
IsNever<T> extends true
25+
? false
26+
: T extends any
27+
? [U] extends [T]
28+
? false
29+
: true
30+
: never
31+
) extends infer Result
32+
// In some cases `Result` will return `false | true` which is `boolean`,
33+
// that means `T` has at least two types and it's a union type,
34+
// so we will return `true` instead of `boolean`.
35+
? boolean extends Result ? true
36+
: Result
37+
: never; // Should never happen

source/shared-union-fields.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import type {NonRecursiveType, IsUnion} from './internal/index.d.ts';
1+
import type {NonRecursiveType} from './internal/index.d.ts';
22
import type {IsNever} from './is-never.d.ts';
3+
import type {IsUnion} from './is-union.d.ts';
34
import type {Simplify} from './simplify.d.ts';
45
import type {UnknownArray} from './unknown-array.d.ts';
56

source/single-key-object.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type {IsEmptyObject} from './empty-object.js';
22
import type {If} from './if.js';
3-
import type {IsUnion} from './internal/index.d.ts';
3+
import type {IsUnion} from './is-union.d.ts';
44

55
/**
66
Create a type that only accepts an object with a single key.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {expectType} from 'tsd';
2-
import type {IsUnion} from '../../source/internal/index.d.ts';
2+
import type {IsUnion} from '../index.d.ts';
33

44
expectType<IsUnion<1>>(false);
55
expectType<IsUnion<true>>(false);

0 commit comments

Comments
 (0)