-
Notifications
You must be signed in to change notification settings - Fork 0
/
01097-medium-isunion.ts
33 lines (30 loc) · 1.05 KB
/
01097-medium-isunion.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'
type cases = [
Expect<Equal<IsUnion<string>, false>>,
Expect<Equal<IsUnion<string | number>, true>>,
Expect<Equal<IsUnion<'a' | 'b' | 'c' | 'd'>, true>>,
Expect<Equal<IsUnion<undefined | null | void | ''>, true>>,
Expect<Equal<IsUnion<{ a: string } | { a: number }>, true>>,
Expect<Equal<IsUnion<{ a: string | number }>, false>>,
Expect<Equal<IsUnion<[string | number]>, false>>,
// Cases where T resolves to a non-union type.
Expect<Equal<IsUnion<string | never>, false>>,
Expect<Equal<IsUnion<string | unknown>, false>>,
Expect<Equal<IsUnion<string | any>, false>>,
Expect<Equal<IsUnion<string | 'a'>, false>>,
Expect<Equal<IsUnion<never>, false>>
]
// ============= Your Code Here =============
// type IsUnion<T, U = T> = [T] extends [never]
// ? false
// : T extends never
// ? false
// : [U] extends [T]
// ? false
// : true
type IsUnion<T, U = T> = (
T extends T ? (U extends T ? true : unknown) : never
) extends true
? false
: true