IsUnion
介绍
实现一个类型IsUnion, 接收一个类型T, 如果解析为联合类型则返回true, 否则返回false
例如:
ts
type case1 = IsUnion<string> // falsetype case2 = IsUnion<string | number> // truetype case3 = IsUnion<[string | number]> // false
View on GitHubts
type case1 = IsUnion<string> // falsetype case2 = IsUnion<string | number> // truetype case3 = IsUnion<[string | number]> // false
起点
ts
/* _____________ Your Code Here _____________ */typeIsUnion <T > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.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>>,// Cases where T resolves to a non-union type.Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.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>>,]
take the challengets
/* _____________ Your Code Here _____________ */typeIsUnion <T > = any/* _____________ Test Cases _____________ */typecases = [Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.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>>,// Cases where T resolves to a non-union type.Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.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>>,]
解决方案
Spoiler warning // Click to reveal answer
ts
typeIsUnion <T > = any
ts
typeIsUnion <T > = any
ts
// my solutiontypeIsUnion <T ,U =T > = (T extendsU ?U extendsT ? false : true : never) extends false ? false : true;
ts
// my solutiontypeIsUnion <T ,U =T > = (T extendsU ?U extendsT ? false : true : never) extends false ? false : true;