02949-hard-objectfromentries

Back

type ObjectFromEntries<T extends [PropertyKey, unknown]> = {
  [P in T as P[0]]: P[1]
}

Solution by 2083335157 #35063

There are better solutions in issues, but submit as another solution.

type ObjectFromEntries<T extends [PropertyKey, unknown]> = {
  [P in T[0]]: (T & [P, unknown])[1];
};

Solution by yukicountry #34475

type ObjectFromEntries<T extends [string, any]> = {
  [K in T as K[0]]: K[1]
}

Solution by ouzexi #34303

type FromEntriesNoUnion<T> = T extends [infer K, infer V]
  ? Record<K & string, V>
  : never;

type Copy<T> = {
  [P in keyof T]: T[P];
};

type ObjectFromEntries<T> = (
  T extends any ? (arg: FromEntriesNoUnion<T>) => void : never
) extends (arg: infer R) => void
  ? Copy<R>
  : never;

Solution by Vampirelee #32642

type ObjectFromEntries<T extends [string, any]> = { [K in T as K[0]]: K[1] };

Solution by vangie #32248

// your answers
type ObjectFromEntries<T extends Array<[string, any]>[number]> = {
  [Key in T as Key[0]]: Key[1]
}


Solution by 437204933 #29707

// your answers
type T1<T extends any[]> = (T extends any ? (args: Record<T[0], T[1]>) => any : never) extends (args: infer A) => any ? A : never
type ObjectFromEntries<T extends any[]> = { [K in keyof T1<T>]: T1<T>[K] }

ε…ˆθ”εˆθ½¬δΊ€ε‰οΌŒη„ΆεŽδΊ€ε‰ε˜ζˆζœ€η»ˆηš„ε―Ήθ±‘

Solution by AAA611 #28897

type UnionToIntersection<T> = (T extends any ? (_: T) => void : never) extends (
  _: infer R
) => void
  ? R
  : never;

type ObjectFromEntries<T> = Omit<
  UnionToIntersection<
    T extends [infer K extends PropertyKey, infer V] ? Record<K, V> : never
  >,
  never
>;

Solution by JohnLi1999 #25866

type ObjectFromEntries<T extends [string, any]> = { [P in T[0]]: T extends [P, infer R] ? R : never };   //Tδ½œδΈΊδΈ€δΈͺ整体攢ηͺ„

// old way
// type UnionToIntersection<T> = (T extends T ? (arg: T) => void : never) extends (arg: infer A) => void ? A : never;
// type CombineObjects<T> = { [P in keyof T]: T[P] };

// type ObjectFromEntries<T extends [PropertyKey, any]> =
//   CombineObjects<
//     UnionToIntersection<
//       T extends [infer K extends PropertyKey, infer V] ? { [P in K]: V } : never    // ['a', A] | ['b', B] | ['c', C] ==> {a: A} | {b: B} | {c: C}
//     >                                                                               // ==> {a: A} & {b: B} & {c: C}
//   >;                                                                                // ==> {a: A, b: B, c: C}

//simple way
// type ObjectFromEntries<T extends [PropertyKey, any]> = { [P in T as P[0]]: P[1] };  //ιεŽ†θ”εˆη±»εž‹

Solution by E-uler #24975

type ObjectFromEntries<T extends [string, unknown]> = {
  [Key in T as Key[0]]: Key[1]
}

Solution by NeylonR #24528

// your answers
type ObjectFromEntries<T extends [string, unknown]> = {
  [Entry in T as Entry[0]]: Entry[1]
}

Solution by jxhhdx #23987

// your answers
type ObjectFromEntries<T extends [string, unknown]> = {
  [Entry in T as Entry[0]]: Entry[1]
}

Solution by snakeUni #23973

type UnionToIntersection<U> = (U extends unknown ? (a: U) => 0 : never) extends ( a: infer I) => 0 ? I : never

type IntersectionToObj<T> = { [K in keyof T]: T[K] }

type IntersectionFromEntries<T extends any[], U extends T = T> = T extends U ? {[K in T[0]]:T[1]} : never

type ObjectFromEntries<T extends any[]> =  IntersectionToObj<UnionToIntersection<IntersectionFromEntries<T>>>

Solution by TKBnice #23367

type ObjectFromEntries<T extends [string, unknown]> = {
  [Entry in T as Entry[0]]: Entry[1]
}

Solution by drylint #22751

type Union2Intersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
type Merge<T> = {
  [K in keyof T]: T[K];
};

type ObjectFromEntries<U> = Merge<
  Union2Intersection<
    U extends any ? (U extends [infer Key extends PropertyKey, infer Types] ? { [K in Key]: Types } : never) : never
  >
>;

// test
interface Model {
  name: string;
  age: number;
  locations: string[] | null;
}

type ModelEntries = ['name', string] | ['age', number] | ['locations', string[] | null];
type result = ObjectFromEntries<ModelEntries>;

Solution by zqiangxu #21955

type ObjectFromEntries<T extends [string, unknown]> = {
  [P in T as P[0]]: P[1]
}

playground

key point:

Solution by zhaoyao91 #21842

// your answers
type ObjectFromEntries<T extends [string, unknown]> = {
  [K in T as K[0]]: K[1];
};

Solution by YqxLzx #21564

type pxx1<T extends [string, any]> = T extends any
  ? {
      [P in T[0]]: T[1];
    }
  : never;

type ObjectFromEntries<T extends [string, any], G = pxx1<T>> = {
  [P in G extends any ? keyof G : any]: G extends any
    ? P extends keyof G
      ? G[P]
      : never
    : never;
} & {};

Solution by so11y #21251

// your answers
type ObjectFromEntries<T extends [string, unknown]> = {
  [K in T as K[0]]: K[1];
};

Solution by fengjinlong #20319

type ObjectFromEntries<T extends [string | number | symbol, unknown]> = {
  [E in T as E[0]]: E[1]
}

Solution by theoolee #19704

type ObjectFromEntries<T> = {
  [p in T as p extends any[] ? (p[0] extends string ? p[0] : never) : never]: p extends any[] ? p[1] : never;
};

Solution by CaoXueLiang #19481

// your answers
type TurnObject<T> = T extends [infer K, infer U]
                        ? { [k in K extends string ? K : never]: U }
                        : never;

type PickKeys<T> = (T extends any 
                        ? (arg: T) => void 
                        : never) extends (arg: infer U) => void 
                                    ? U
                                    : never

type ObjectFromEntries<T> = {
    [K in keyof PickKeys<TurnObject<T>>]: PickKeys<TurnObject<T>>[K]
}

Solution by jiaaoMario #17534

type ObjectFromEntries<T extends [PropertyKey, unknown]> = {
  [P in T as P[0]]: P[1]
}

Solution by BulatDashiev #16950

// your answers

/**
 * UnionToFunc<1 | 2> => ((arg: 1) => void | (arg: 2) => void)
 */
 type UnionToFunc<T> = T extends unknown ? (arg: T) => void : never

 /**
  * UnionToIntersection<1 | 2> = 1 & 2
  */
 type UnionToIntersection<U> = UnionToFunc<U> extends (arg: infer Arg) => void
   ? Arg
   : never
 
 /**
  * LastInUnion<1 | 2> = 2
  */
 type LastInUnion<U> = UnionToIntersection<UnionToFunc<U>> extends (x: infer L) => void
   ? L
   : never
 
 type UnionToTuple<T, L = LastInUnion<T>> = [L] extends [never]
   ? []
   : [...UnionToTuple<Exclude<T, L>>, L]

type ObjectFromEntries<T, D = UnionToTuple<T>, Result extends Record<PropertyKey, any> = {}> = D extends [infer F, ...infer R]
  ? F extends [infer FF extends string, infer FR]
    ? ObjectFromEntries<
        never,
        R,
        {
          [K in FF | keyof Result]: K extends FF
            ? FR
            : K extends keyof Result
              ? Result[K]
            : never
        }
      >
    : never
  : Result

Solution by humandetail #16491

// your answers
type ObjectFromEntries<
    E extends readonly [PropertyKey,any] > = 
    {[J in E as J[0]] : J[1]} ;

Solution by justBadProgrammer #15992

// your answers
type Merge<
    T extends object > =
    {[J in keyof T] : T[J]} ;

type UnionToIntersection<
    U> =
    (U extends any ? (arg: U)=>any : never) extends (arg: infer R)=>any ? R : any ;

type UnionFromEntries<
    E extends readonly [PropertyKey,any] > = 
    E extends [infer R extends PropertyKey,infer U]
    ? {[J in R] : U}
    : never ;

    type ObjectFromEntries<
    E extends readonly [PropertyKey,any] > = 
    Merge<UnionToIntersection<UnionFromEntries<ModelEntries>>>  ;

Solution by justBadProgrammer #15991

// your answers
type ObjectFromEntries<T extends [string, unknown]> = {
  [K in T['0']]: T extends [K, infer Value] ? Value : never
}

Solution by liuxing95 #13123

// your answers

type ArrToObj<T> = T extends T 
  ? T extends [infer Key, infer Val]
    ? { [K in Key & string]: Val}
    : never
  : never

type GetK<T> = T extends T 
  ? T extends [infer Key, infer Val]
    ? Key & string
    : never
  : never

type UnionTointersection<T> = (T extends T ? (x: T) => void : never) extends (x: infer R) => void
  ? R
  : never

type ObjectFromEntries<T> = {
    [Key in GetK<T>]: Key extends keyof UnionTointersection<ArrToObj<T>>
      ? UnionTointersection<ArrToObj<T>>[Key]
      : never
  }

Solution by GumplinGo #13037

type ObjectFromEntries<TEntries extends [string, unknown]> = {
  [key in TEntries[0]]: TEntries extends [key, infer Value] ? Value : never;
};

Solution by michaltarasiuk #12403

type ObjectFromEntries<T> =
  {
    [P in T & [keyof any, unknown]as P[0]]:
    P[1]
  }

Playground

Solution by teamchong #11803