File tree Expand file tree Collapse file tree 7 files changed +43
-38
lines changed
Expand file tree Collapse file tree 7 files changed +43
-38
lines changed Original file line number Diff line number Diff line change @@ -144,6 +144,7 @@ export type {Or} from './source/or.d.ts';
144144export type { NonEmptyTuple } from './source/non-empty-tuple.d.ts' ;
145145export type { FindGlobalInstanceType , FindGlobalType } from './source/find-global-type.d.ts' ;
146146export type { If } from './source/if.d.ts' ;
147+ export type { IsUnion } from './source/is-union.d.ts' ;
147148
148149// Template literal types
149150export type { CamelCase } from './source/camel-case.d.ts' ;
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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/**
11883An if-else-like type that resolves depending on whether the given type is `any` or `never`.
11984
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 1- import type { NonRecursiveType , IsUnion } from './internal/index.d.ts' ;
1+ import type { NonRecursiveType } from './internal/index.d.ts' ;
22import type { IsNever } from './is-never.d.ts' ;
3+ import type { IsUnion } from './is-union.d.ts' ;
34import type { Simplify } from './simplify.d.ts' ;
45import type { UnknownArray } from './unknown-array.d.ts' ;
56
Original file line number Diff line number Diff line change 11import type { IsEmptyObject } from './empty-object.js' ;
22import 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/**
66Create a type that only accepts an object with a single key.
Original file line number Diff line number Diff line change 11import { expectType } from 'tsd' ;
2- import type { IsUnion } from '../../source/internal/ index.d.ts' ;
2+ import type { IsUnion } from '../index.d.ts' ;
33
44expectType < IsUnion < 1 > > ( false ) ;
55expectType < IsUnion < true > > ( false ) ;
You can’t perform that action at this time.
0 commit comments