[Typescript] 129. Hard - Capitalize Nest Object Keys

Capitalize the key of the object, and if the value is an array, iterate through the objects in the array.

 

/* _____________ Your Code Here _____________ */

type CapitalizeNestObjectKeys<T> = T extends any[]
    ? T extends [infer F, ...infer R]
      ? [CapitalizeNestObjectKeys<F>, ...CapitalizeNestObjectKeys<R>]
      : []
    : T extends object
      ? { [K in keyof T as K extends string ? Capitalize<K> : never]: CapitalizeNestObjectKeys<T[K]> }
      : T


/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type foo = {
  foo: string
  bars: [{ foo: string }]
}

type Foo = {
  Foo: string
  Bars: [{
    Foo: string
  }]
}
type cases = [
  Expect<Equal<Foo, CapitalizeNestObjectKeys<foo>>>,
]

 

posted @   Zhentiw  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-12-06 [Javascript] Broadcaster + Operator + Listener pattern -- 23. ifElse operator
2019-12-06 [Algorithm] 206. Reverse Linked List
2018-12-06 [Cypress] install, configure, and script Cypress for JavaScript web applications -- part3
2016-12-06 [Angular2] Map keyboards events to Function
2016-12-06 [Vue] Create Vue.js Layout and Navigation with Nuxt.js
2016-12-06 [Now] Deploy a Node project with Zeit’s Now
2016-12-06 [React] Create & Deploy a Universal React App using Zeit Next
点击右上角即可分享
微信分享提示