[Typescript] 54. Medium - Tuple to Nested Object
Given a tuple type T
that only contains string type, and a type U
, build an object recursively.
type a = TupleToNestedObject<['a'], string> // {a: string}
type b = TupleToNestedObject<['a', 'b'], number> // {a: {b: number}}
type c = TupleToNestedObject<[], boolean> // boolean. if the tuple is empty, just return the U type
/* _____________ Your Code Here _____________ */
type TupleToNestedObject<T extends any[], U> = T extends [infer A extends string, ...(infer REST)]
? {[Key in A]: TupleToNestedObject<REST, U>}
: U
type x = TupleToNestedObject<['a', 'b'], number>
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<TupleToNestedObject<['a'], string>, { a: string }>>,
Expect<Equal<TupleToNestedObject<['a', 'b'], number>, { a: { b: number } }>>,
Expect<Equal<TupleToNestedObject<['a', 'b', 'c'], boolean>, { a: { b: { c: boolean } } }>>,
Expect<Equal<TupleToNestedObject<[], boolean>, boolean>>,
]
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2021-10-17 [CSS] Tailwindcss: get started
2021-10-17 [CSS] Purgecss to remove unused css
2019-10-17 [TypeScript] Modifier
2019-10-17 [Flutter] Getting Location Data From Across Platforms
2018-10-17 [Javascript] Link to Other Objects through the JavaScript Prototype Chain
2017-10-17 [Redux-Observable && Unit Testing] Use tests to verify updates to the Redux store (rxjs scheduler)
2017-10-17 [Redux-Observable && Unit Testing] Mocking an ajax request when testing epics