Is it possible to create the type Cartesian such that Cartesian<'a' | 'b' | 'c'> equals the type
'ab' | 'ba' | 'ac' | 'ca' | 'bc' | 'cb'
Partial solution: if we define type Cartesian<T extends string> = `${T}_${T}`; we also get 'aa', 'bb', and 'cc'. Is there a way to remove those? Perhaps using Exclude?
>Solution :
One way you can create the required Cartesian type by using the Extract utility type to exclude the undesired combinations. Something like:
type Cartesian<T extends string> = {
[K1 in T]: {
[K2 in T]: K1 extends K2 ? never : `${K1}${K2}`;
}[T];
}[T];
type Result = Cartesian<'a' | 'b' | 'c'>;
// Result: 'ab' | 'ac' | 'ba' | 'bc' | 'ca' | 'cb'