๐ Trying some stuff with template literals:
type UnionA = { type: 'a', title: string };
type UnionB = { type: 'b', message: string };
type Both = UnionA | UnionB
type StringifyInner<K extends string | number, T> = T extends string | number ? `${K}` : Stringify<T>
type Stringify<T extends object> = {
[K in keyof T]-?: StringifyInner<K, T[K]>;
}[keyof T];
type A = Stringify<Both>
// ^? type A = "type"
// I want this to be "type" | "title" | "message"
The Stringify type constrains the result to the intersection of the 2, but I want all of them
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.