[TypeScript] Type Challenges #3188 - Tuple to Nested Object

22 阅读1分钟

题目描述

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

题解

// ============= Test Cases =============
import type { Equal, Expect } from './test-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>>,
]


// ============= Your Code Here =============
type TupleToNestedObject<
  T extends string[],
  U
> =
	T extends [
      infer Head extends string, 
      ...infer Tail extends string[]
	]
      ? {
			[P in Head]: TupleToNestedObject<Tail, U>
        }
      : U

条件类型

  • T extends [infer Head extends string, ...infer Tail extends string[]] ? ... : ...

    • 如果T是非空数组:

      • 使用infer Head推导数组的第一个元素,并确保它是字符串类型

      • 使用...infer Tail推导数组的剩余部分,并确保它是字符串数组

      • 通过{ [P in Head]: TupleToNestedObject<Tail, U> }创建一个对象类型:

        • 对象的键是Head

        • 对象的值是递归调用TupleToNestedObject<Tail, U>的结果

    • 如果T是空数组:

      • 直接返回类型U