Is it possible to check if a given type is a union?
type IsUnion<T> = ???
Why I need this: in my code, I have the only case when some received type can be a union. I handle it with a distributive conditional type. However, it can be not obvious for one who looks at this code why a DCT is used in the first place. So I want it to be explicit like: IsUnion<T> extends true ? T extends Foo ...
I've made a few attempts with UnionToIntersection, with no results. I've also come up with this one:
type IsUnion<T, U extends T = T> =
T extends any ?
(U extends T ? false : true)
: never
It gives false for non unions, but for some reason it gives boolean for unions... And I have no idea why. I also tried to infer U from T, with no success.
P.S. My use case may seem to someone as not perfect/correct/good, but anyway the question in the title has arised and I wonder if it's possible (I feel that it is, but am having hard time to figure it out myself).