My goal is to create a mapping between the keys of an object type and another object, following these rules:
- Each key should be from the original type
- If the value's type is a union type, it should have { enum: Array }
- If the value's type is not a union type, it should have { enum: never }
For example:
type OriginalType = {
foo: "a" | "b";
bar: "c";
foobar: Record<string, string>;
};
type MappedType<T> = { ... };
type ResultType = MappedType<OriginalType>;
// ResultType = {
// foo: { enum: ["a", "b"] },
// bar: { enum: never },
// foobar: { enum: never },
//}
I am able to map the keys and values to a new type, but I'm struggling with determining whether a type is a union type:
type IsUnion<T> = ...
type MappedType<T> = {
[K in keyof T]: IsUnion<T[K]> extends true ? { enum: Array<T[K]> } : never;
};