Supposing the abc
type that is an union of the elements 'a', 'b', 'c'
, I'd like to know if is possible to extract any single element from this union, like the example below.
type ExtractRandomElementOfSet<T> = ...
type abc = 'a' | 'b' | 'c';
let testType: ExtractRandomElementOfSet<abc>; //=> "b" (or "a", or "c", whatever)
I know that is possible to extract the "last" element. I put between quotation marks because if it is a set, then there is nothing to talk about first, second, third... last element.
TypeScript does not intend to allow you to choose an element from an arbitrary union type at all, let alone allowing you to choose one "at random".
The supported language features for unions generally only allow you to do "the same thing" to each member, independently. For example, a distributive conditional type splits a union into its members, performs some single computation on each member, and then collects them back into a union. Or you can use key remapping in mapped types to iterate union members, perform a single key-and-value computation on each member, and collect them back into an object type. But you're not supposed to be able to distinguish union members in arbitrary unions in such a way as to discover or impose an order on them.
Now, it turns out that there are several loopholes in the language that effectively let you choose an element from a union. The question How to transform union type to tuple type has an answer that describes how to do it. But that answer also states, as forcefully as I could do so, that this is not supported. And therefore it ought not to be very surprising that the method for doing so is circuitous and inefficient.
Indeed, if someone asked how they could get an expensive diamond ring from a jewelry store without paying for it, it is quite possible to describe a procedure that does this, but it is going to be more circuitous and inefficient than pushing a "free diamond ring please" button on the side of the building. So you have to sneak in through the ventilation system from an adjacent business and then disable the alarm system with a keycard you duplicated earlier in the day by distracting the proprietor with a runaway dog, etc., for the same reason: you're not supposed to be doing that.
Every time someone has asked for a feature in TypeScript which would allow you to, (directly or indirectly) select a single element from a union, this request has been declined. Thou shalt not convert a union to a tuple (microsoft/TypeScript#13298). Thou shalt not perform an order-dependent "for loop" over unions (microsoft/TypeScript#44116). Thou shalt not observe the "first" or "last" element of a union (microsoft/TypeScript#56813). You can break these commandments, but TypeScript isn't going to try to help you do so.