00011-easy-tuple-to-object

68 阅读1分钟

问题

Given an array, transform it into an object type and the key/value must be in the provided array.

For example:

const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const

type result = TupleToObject<typeof tuple> // expected { 'tesla': 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'}

解答

type TupleToObject<T extends readonly any[]> = { [P in T[number]]: P }