type-challenges
type-challenges copied to clipboard
3188 - Tuple to Nested Object
type TupleToNestedObject<T, U> = T extends [infer F,...infer R]?
{
[K in F&string]:TupleToNestedObject<R,U>
}
:U
what means F&string can you explain please? Is it equal A extends string ? A : never
@motionrus
Yes, it is equal to your code which means narrowing type of K into F when F is string.
type TupleToNestedObject<T, U> = T extends [infer F extends PropertyKey,...infer R]?
{
[K in F] : TupleToNestedObject<R,U>
}
:U
type TupleToNestedObject<T, U> = T extends [infer F extends PropertyKey,...infer R]? { [K in F] : TupleToNestedObject<R,U> } :U
能解释下这个PropertyKey在这里的作用吗
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 官方提供的类型
so nice
type TupleToNestedObject<T extends any[], U> =
T extends [infer F, ...infer B] ?
{
[k in F & PropertyKey]: TupleToNestedObject<B, U>
}
: U