ghaiklor / type-challenges-solutions

Solutions for the collection of TypeScript type challenges with explanations
https://ghaiklor.github.io/type-challenges-solutions/
Creative Commons Attribution 4.0 International
470 stars 56 forks source link

type-challenges-solutions/en/medium-isunion #115

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

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

EmmaammE commented 2 years ago

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

ghaiklor commented 2 years ago

@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 👍🏻

MajorLift commented 1 year ago

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