1 Star 0 Fork 0

Sunny / type-challenges

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.zh-CN.md 3.54 KB
一键复制 编辑 原始数据 按行查看 历史
antfu 提交于 2023-01-11 00:37 . chore: update index

Tuple to Enum Object 困难 #tuple #template-literal

by Ryo Hanafusa @softoika

接受挑战    English 日本語

枚举是 TypeScript 的一种原生语法(在 JavaScript 中不存在)。因此在 JavaScript 中枚举会被转成如下形式的代码:

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

在这个问题中,你实现的类型应当将给定的字符串元组转成一个行为类似枚举的对象。此外,枚举的属性一般是 pascal-case 的。

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

如果传递了第二个泛型参数,且值为 true,那么返回值应当是一个 number 字面量。

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

返回首页 分享你的解答 查看解答

相关挑战

10・元组转合集 11・元组转换为对象 730・Union to Tuple 3188・Tuple to Nested Object
1
https://gitee.com/ztes/type-challenges.git
git@gitee.com:ztes/type-challenges.git
ztes
type-challenges
type-challenges
main

搜索帮助

53164aa7 5694891 3bd8fe86 5694891