Question

Give an array, transform into an object type and the key/value must in the given array.
For example

  1. const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const
  2. type result = TupleToObject<typeof tuple> // expected { tesla: 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'}

Answer

  1. type TupleToObject<T extends readonly any[]> = {
  2. [K in T[number]]: K;
  3. }