Skip to main content

1097 - IsUnion

Answer TestCases

实现类型 IsUnion,判断类型 T 是否是联合类型

例如:

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

Solution

type IsUnion<T, U = T> = [T] extends [never]
? false
: T extends U
? [U] extends [T]
? false
: true
: never