[Typescript] 104. Hard - Tuple to Enum Object
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 }
/* _____________ Your Code Here _____________ */
type FindIndex<T extends readonly any[], K, ACC extends unknown[] = []> = T extends readonly [infer F, ...infer RT]
? K extends F
? ACC['length']
: FindIndex<RT, K, [...ACC, unknown]>
: -1;
type Enum<T extends readonly string[], N extends boolean = false> = {
readonly [Key in T[number] as `${Capitalize<Key>}`]: N extends false ? Key : FindIndex<T, Key>
}
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
const OperatingSystem = ['macOS', 'Windows', 'Linux'] as const
const Command = ['echo', 'grep', 'sed', 'awk', 'cut', 'uniq', 'head', 'tail', 'xargs', 'shift'] as const
type cases = [
Expect<Equal<Enum<[]>, {}>>,
Expect<Equal<
Enum<typeof OperatingSystem>,
{
readonly MacOS: 'macOS'
readonly Windows: 'Windows'
readonly Linux: 'Linux'
}
>>,
Expect<Equal<
Enum<typeof OperatingSystem, true>,
{
readonly MacOS: 0
readonly Windows: 1
readonly Linux: 2
}
>>,
Expect<Equal<
Enum<typeof Command>,
{
readonly Echo: 'echo'
readonly Grep: 'grep'
readonly Sed: 'sed'
readonly Awk: 'awk'
readonly Cut: 'cut'
readonly Uniq: 'uniq'
readonly Head: 'head'
readonly Tail: 'tail'
readonly Xargs: 'xargs'
readonly Shift: 'shift'
}
>>,
Expect<Equal<
Enum<typeof Command, true>,
{
readonly Echo: 0
readonly Grep: 1
readonly Sed: 2
readonly Awk: 3
readonly Cut: 4
readonly Uniq: 5
readonly Head: 6
readonly Tail: 7
readonly Xargs: 8
readonly Shift: 9
}
>>,
]
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2019-11-16 [Flutter] Style a message chat style-ish bubble
2019-11-16 [Flutter] Flexible the Widget height to available space
2019-11-16 [Flutter] Clear a TextField after send button
2019-11-16 [Flutter] Using StreamBuilder to build a List of Text Widget based on the data from Firebase
2016-11-16 [MobX] MobX fundamentals: deriving computed values and managing side effects with reactions
2014-11-16 [Grunt] External Config
2014-11-16 [Grunt] Uglify