type-challenges icon indicating copy to clipboard operation
type-challenges copied to clipboard

3188 - Tuple to Nested Object

Open jiangshanmeta opened this issue 4 years ago • 5 comments

type TupleToNestedObject<T, U> = T extends [infer F,...infer R]?
  {
    [K in F&string]:TupleToNestedObject<R,U>
  }
  :U

jiangshanmeta avatar Sep 14 '21 13:09 jiangshanmeta

what means F&string can you explain please? Is it equal A extends string ? A : never

motionrus avatar May 30 '22 05:05 motionrus

@motionrus

Yes, it is equal to your code which means narrowing type of K into F when F is string.

baeharam avatar Jun 03 '23 08:06 baeharam

type TupleToNestedObject<T, U> = T extends [infer F extends PropertyKey,...infer R]?
  {
    [K in F] : TupleToNestedObject<R,U>
  }
  :U

Huauauaa avatar Mar 13 '24 03:03 Huauauaa

type TupleToNestedObject<T, U> = T extends [infer F extends PropertyKey,...infer R]?
  {
    [K in F] : TupleToNestedObject<R,U>
  }
  :U

能解释下这个PropertyKey在这里的作用吗

ZhipengYang0605 avatar May 13 '24 14:05 ZhipengYang0605

type TupleToNestedObject<T, U> = T extends [infer F extends PropertyKey,...infer R]?
  {
    [K in F] : TupleToNestedObject<R,U>
  }
  :U

能解释下这个PropertyKey在这里的作用吗

declare type PropertyKey = string | number | symbol 官方提供的类型

Huauauaa avatar May 14 '24 00:05 Huauauaa

so nice

type TupleToNestedObject<T extends any[], U> =

  T extends [infer F, ...infer B] ?
  {
    [k in F & PropertyKey]: TupleToNestedObject<B, U>
  }
  : U

Jayce-liang avatar Sep 29 '24 15:09 Jayce-liang