[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 @ 2022-12-06 21:24  Zhentiw  阅读(20)  评论(0编辑  收藏  举报