跳到主要内容

IsUnion

介绍

实现一个类型IsUnion, 接收一个类型T, 如果解析为联合类型则返回true, 否则返回false

例如:

ts
type case1 = IsUnion<string> // false
type case2 = IsUnion<string | number> // true
type case3 = IsUnion<[string | number]> // false
ts
type case1 = IsUnion<string> // false
type case2 = IsUnion<string | number> // true
type case3 = IsUnion<[string | number]> // false
View on GitHub

起点

ts
/* _____________ Your Code Here _____________ */
 
type IsUnion<T> = any
 
/* _____________ Test Cases _____________ */
 
 
type cases = [
Expect<Equal<IsUnion<string>, false>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IsUnion<string | number>, true>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IsUnion<'a' | 'b' | 'c' | 'd'>, true>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IsUnion<undefined | null | void | ''>, true>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IsUnion<{ a: string } | { a: number }>, true>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IsUnion<{ a: string | number }>, false>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IsUnion<[string | number]>, false>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
// Cases where T resolves to a non-union type.
Expect<Equal<IsUnion<string | never>, false>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IsUnion<string | unknown>, false>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IsUnion<string | any>, false>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IsUnion<string | 'a'>, false>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IsUnion<never>, false>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
ts
/* _____________ Your Code Here _____________ */
 
type IsUnion<T> = any
 
/* _____________ Test Cases _____________ */
 
 
type cases = [
Expect<Equal<IsUnion<string>, false>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IsUnion<string | number>, true>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IsUnion<'a' | 'b' | 'c' | 'd'>, true>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IsUnion<undefined | null | void | ''>, true>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IsUnion<{ a: string } | { a: number }>, true>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IsUnion<{ a: string | number }>, false>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IsUnion<[string | number]>, false>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
// Cases where T resolves to a non-union type.
Expect<Equal<IsUnion<string | never>, false>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IsUnion<string | unknown>, false>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IsUnion<string | any>, false>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IsUnion<string | 'a'>, false>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<IsUnion<never>, false>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
take the challenge

解决方案

Spoiler warning // Click to reveal answer
ts
type IsUnion<T> = any
 
 
ts
type IsUnion<T> = any
 
 
ts
// my solution
type IsUnion<T, U=T> = (T extends U ? U extends T ? false : true : never) extends false ? false : true;
 
ts
// my solution
type IsUnion<T, U=T> = (T extends U ? U extends T ? false : true : never) extends false ? false : true;
 
view more solutions