Type Challenges Judge

IsUnion

提出詳細

type Helper<T,K=T> = T[] extends never[] ? [] : K extends T ? [unknown,...Helper<Exclude<T,K>>] : [unknown] type IsUnion<T> = Helper<T>["length"] extends 0 | 1 ? false : true
提出日時2023-09-14 14:04:57
問題IsUnion
ユーザーsankantsu
ステータスAccepted
テストケース
import type { 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 >>, ]