Tuple to Enum Object

Challenge

The enum is an original syntax of TypeScript (it does not exist in JavaScript). So it is converted to like the following form as a result of transpilation:

let OperatingSystem;
(function (OperatingSystem) {
  OperatingSystem[(OperatingSystem["MacOS"] = 0)] = "MacOS";
  OperatingSystem[(OperatingSystem["Windows"] = 1)] = "Windows";
  OperatingSystem[(OperatingSystem["Linux"] = 2)] = "Linux";
})(OperatingSystem || (OperatingSystem = {}));

In this question, the type should convert a given string tuple to an object that behaves like an enum. Moreover, the property of an enum is preferably a pascal case.

Enum<["macOS", "Windows", "Linux"]>;
// -> { readonly MacOS: "macOS", readonly Windows: "Windows", readonly Linux: "Linux" }

If true is given in the second argument, the value should be a number literal.

Enum<["macOS", "Windows", "Linux"], true>;
// -> { readonly MacOS: 0, readonly Windows: 1, readonly Linux: 2 }

Solution

!!! TODO

type MergeObjects<T, U> = {[Key in keyof T | keyof U]: Key extends keyof T ? Key extends keyof U ? T[Key] extends true ? T[Key]: U[Key] extends T[Key] ? U[Key]: [T[Key], U[Key]] : T[Key] : Key extends keyof U ? U[Key] : never} type MergeObject = {[Key in keyof T]: T[Key]} type TupleToObject<T extends readonly string[]> = { readonly [key in T[number] as Capitalize]: Extract<T[number], key> } type TupleToObjectNumbers<T extends readonly string[], Count extends number[] = []> = T extends [infer H extends string, …infer R extends string[]] ? Record<Capitalize, Count[‘length’]> & TupleToObjectNumbers<R, […Count, 1]> : {} type Enum<T extends readonly string[], N extends boolean = false> = N extends true ? MergeObject<TupleToObjectNumbers> : TupleToObject

type GG = Enum<[‘macOS’, ‘Windows’, ‘Linux’]> type FF = TupleToObjectNumbers<[‘macOS’, ‘Windows’, ‘Linux’]>

References