Can someone explain the method signature below:
export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
This was the answer to a question about converting a union to an intersection type in ts: https://stackoverflow.com/a/50375286/457156
However I cannot follow the method signature.
Let's break it down:
Ternary operator as an argument
(U extends any ? (k: U) => void : never)
is this saying "if an object is provided, then the expected argument should be a function (k: U) => void
extends
(U extends any ? (k: U) => void : never) extends (k: infer I)
infer
I presume infers the type of I
The rest I am completely baffled by.
Any help would be appreciated. Here is a discussion thread that I didn't quite follow: https://github.com/Microsoft/TypeScript/issues/27907