[Typescript] 151. Medium IsUnion

Zhentiw發表於2024-05-13

Implement a type IsUnion, which takes an input type T and returns whether T resolves to a union type.

For example:

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

Answer:

export type Equal<T, U> = (<P>(x: P) => P extends T ? 1 : 2) extends <P>(
  x: P
) => P extends U ? 1 : 2
  ? true
  : false;
type NotEqual<X, Y> = true extends Equal<X, Y> ? false : true;
type IsUnion<T> = NotEqual<[T] & T, (T extends T ? [T] : never) & T>;

/* _____________ Test Cases _____________ */
import type { NotEqual, Equal, Expect } from '@type-challenges/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>>,
]

We use string as an example

[T] & T is [string] & string

(T extends T ? [T] : never) & T is also [string] & string
If it's union, for example string | number
[T] & T is [string | number] & (string | number)

(T extends T ? [T] : never) & T is ([string]|[number]) & (string | number)

So [string | number]is not equal to ([string] | [number])

相關文章