How can I go about implementing this?
type ActionNames = 'init' | 'reset';
type UnionToObj<U> = {/* SOLUTION NEEDED HERE */}
type Result = UnionToObj<ActionNames>;
// Expected type for Result: `{ init: any, reset: any }`
I tried to write an implementation, but it didn't work as expected. It ran into the issue of union types extending covariance:
type UnionToObj<U> = U extends string ? { [K in U]: any } : never;
type Result = UnionToObj<'init' | 'reset'>;
// The expected Result type is `{ init: any, reset: any }`
// However, I ended up with a union object: `{ init: any } | { reset: any }`
// How can I fix this problem?
Main problems that need resolution:
- Converting string union types to object types
- Covariance of unions in TypeScript's extends clause