1 Star 0 Fork 0

Sunny / type-challenges

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

Tuple to Enum Object 上級 #tuple #template-literal

by Ryo Hanafusa @softoika

挑戦する    English 简体中文

enum は TypeScript 独自の文法です(JavaScript にはありません)。そのため、以下の形式にトランスパイルされます。

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

この問題では、文字列のタプルを enum と同じようなオブジェクトに変換する型を実装します。 さらに、enum のプロパティはパスカルケースであることが好ましいです。

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

第 2 引数にtrueが与えられた場合、値は数値リテラルとなります。

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

戻る 解答を共有 解答を確認

関連する課題

10・Tuple to Union 11・Tuple to Object 730・Union to Tuple 3188・Tuple to Nested Object
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ztes/type-challenges.git
git@gitee.com:ztes/type-challenges.git
ztes
type-challenges
type-challenges
main

搜索帮助

Bbcd6f05 5694891 0cc6727d 5694891