Skip to content

Latest commit

 

History

History
25 lines (22 loc) · 3.36 KB

File metadata and controls

25 lines (22 loc) · 3.36 KB

Tuple to Enum Object hard #tuple #template-literal

by Ryo Hanafusa @softoika

Take the 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 }

Back Share your Solutions Check out Solutions

Related Challenges

10・Tuple to Union 11・Tuple to Object 730・Union to Tuple 3188・Tuple to Nested Object