This is a straightforward mapped type:
type UnionToObj<U extends PropertyKey> = { [K in U]: any }
type Result = UnionToObj<ActionNames>;
/* type Result = {
init: any;
reset: any;
} */
Here we are constraining U
to be key-like instead of checking it via a conditional type. If you really want to do it that way, you can, with a solution like this:
type UnionToObj<U> = [U] extends [PropertyKey] ? { [K in U]: any } : never;
The difference between this and your version is that your version is unintentionally a distributive conditional type. Since you don't want union inputs to become union outputs, you need to prevent conditional type distribution by not having U extends ...
directly with a bare type parameter in the checked position. Wrapping the checked type in a one-tuple ([U] extends ...
) is enough to turn off union distribution.
Playground link to code
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…