type-challenges-solutions icon indicating copy to clipboard operation
type-challenges-solutions copied to clipboard

type-challenges-solutions/en/medium-isunion

Open utterances-bot opened this issue 3 years ago • 3 comments
trafficstars

IsUnion

This project is aimed at helping you better understand how the type system works, writing your own utilities, or just having fun with the challenges.

https://ghaiklor.github.io/type-challenges-solutions/en/medium-isunion.html

utterances-bot avatar Dec 30 '21 03:12 utterances-bot

Thanks for this article, but there are some cases in which this solution doesn't work. I know it from this link: https://stackoverflow.com/a/59687759

EmmaammE avatar Dec 30 '21 04:12 EmmaammE

@EmmaammE yeah, there are some cases when the community adds more test cases after I've solved them and explained. Which brings more requirements and (sometimes) breaks the solution. That's why comments exists 😺

Thanks for the link and letting us know 👍🏻

ghaiklor avatar Jan 07 '22 19:01 ghaiklor

Updated solution for new test case:

type newTestCase = Expect<Equal<IsUnion<never>, false>>

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

MajorLift avatar Jan 02 '23 00:01 MajorLift