#Merge all possible results of a template literal union

11 messages ยท Page 1 of 1 (latest)

severe lichen
#

๐Ÿ‘‹ 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

https://www.typescriptlang.org/play?#code/C4TwDgpgBAqgdgSwPZwIJQLxQN5VJALigHIBDYgGjwWABsIiBnYAJwTgHMoBfAbgCh80eMjgAhTDjzgGJAEaUoAWwiNGpDrOZtOPAYJlQxSYAAtJIlOgA+sRCjH8DkKAGVW7DggBmIAJJwcBAsADwA0lAQAB7AEHAAJoxQ2p5QtnAArkpywVQAKgB8knmRMXGJyR666Vk5LFAA-FAABgAk2GHczVBE7jpeviGFTkJuVQMgQ6WxCUlIcgBWEADGwEVY2PxQUADaEexQANYQIEjeUHkAugC0Db3jPv6BweH5e5cFAtw7x6fnV-pRugsH1PI8QsYzAUtlAAPSwqAAPQaMPhUD8UAA7qQ4MA8KYEElgEgoDkoAAiITktIU4A0ejU2zklRqDQQclAA

feral pumice
severe lichen
#

Thanks, @feral pumice , what should the condition extend on?

feral pumice
#

T extends any or T extends T is common

severe lichen
#

Sorry I'm not understanding, where would that go?

type Stringify<T extends object> = {
  [K in keyof T]-?: StringifyInner<K, T[K]>;
}[keyof T];
feral pumice
#
type Stringify<T extends object> = T extends T ? {
  [K in keyof T]-?: StringifyInner<K, T[K]>;
}[keyof T] : never;
severe lichen
#

Oh amazing, that works! Thanks

vestal lantern
#

!hb distribution

pure fulcrumBOT
vestal lantern
#

it's this btw ^

#

!resolved