00011-easy-tuple-to-object

Back

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

Solution by onoxm #38428

// your answers

Solution by tcyzz94 #38424

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

Solution by ssviaam #38378

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

Solution by M2S-SD #38358

/*
T[number] extracts all element types from the tuple as a union.

Example:
T = ['tesla', 'model 3', 'model X']

T[number]
=> 'tesla' | 'model 3' | 'model X'

Then the mapped type iterates over each value in that union
and uses it as both the object key and value.
*/
type TupleToObject<T extends readonly (string | number | symbol)[]> = {
  [P in T[number]]: P
}

Solution by VarunMendre #38328

type TupleToObject<T extends readonly string[]> = {
	[Key in T[number]]: Key;
};

Solution by Fzgt #38266

// your answers
type TupleToObject<T extends readonly PropertyKey[]> = {
  [key in T[number]]: key
}

Solution by jospinchimi #38229

// your answers

Solution by Kotkoa #38216

// 여기 풀이를 입력하세요
type TupleToObject<T extends readonly PropertyKey[]> = {
  [K in T[number]] : K 
}

Solution by kyu-rong #38196

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

Solution by Tofu-Xx #38116

type arrayToObject<T extends readonly (string | number | symbol)[]> = {
  [Key in T[number]] : Key
}

type TupleToObject<T extends readonly (string | number | symbol)[]> = arrayToObject<T> 

Here I'm restricting T to be an array of (string | number | symbol ) array ,

and then in the Key I'm able to refer to the individual elements and in the right hand side I can say that value is also the key

Solution by mainakgupta33 #38066

type TupleToObject<T extends readonly (string | symbol | number)[]> = {
    [K in T[number]]: K
}

Solution by Charlotte-n #38062

type TupleToObject<T extends readonly (string | number | symbol)[]> = {
  [K in T[number]]: K
}

Solution by Nikita0x #38022

type TupleToObject<T extends readonly PropertyKey[]> = {
  [key in T[number]]: key;
}

Solution by seungjae0619 #37985

// your answers
type TupleToObject<T extends readonly PropertyKey[]> = { [K in T[number]]: K };

Solution by istamal #37885

type TupleToObject<T extends readonly (keyof any)[]> = {
  [k in T[number]] : k
}

Solution by tac-tac-go #37884

type TupleToObject<T extends readonly string[]> = {
	[K in T[number]]: K;
};

Solution by ClarityOfMind #37874

/* _____________ Your Code Here _____________ */
// @show-types
type TupleToObject<T extends readonly any[]> = { [K in T[number]]: K }

/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'


const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const
const tupleNumber = [1, 2, 3, 4] as const
const sym1 = Symbol(1)
const sym2 = Symbol(2)
const tupleSymbol = [sym1, sym2] as const
const tupleMix = [1, '2', 3, '4', sym1] as const

type cases = [
  Expect<Equal<TupleToObject<typeof tuple>, { 'tesla': 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y' }>>,
  Expect<Equal<TupleToObject<typeof tupleNumber>, { 1: 1, 2: 2, 3: 3, 4: 4 }>>,
  Expect<Equal<TupleToObject<typeof tupleSymbol>, { [sym1]: typeof sym1, [sym2]: typeof sym2 }>>,
  Expect<Equal<TupleToObject<typeof tupleMix>, { 1: 1, '2': '2', 3: 3, '4': '4', [sym1]: typeof sym1 }>>,
]

type error = TupleToObject<[[1, 2], {}]>

Solution by tuannvnguyen #37869

type TupleToObject<T extends readonly (string | number | symbol)[]> = {
  [P in T[number]]: P
}

Solution by Hansol46 #37834

type TupleToObject<T extends readonly (string | number | symbol)[]> = {
    [K in T[number]]: K
}

Solution by pkutsenko #37820

type TupleToObject<T extends readonly PropertyKey[]> = {
  [K in T[number]]: K
}

Solution by jisooooooooooo #37780

// your answers

Solution by Brahima-Fofana #37757

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

/**
 * 补充:
 * T[number]: number 是一个关键字,它的潜台词是:“不管下标是 0 还是 1 还是 100,只要下标是数字(number),它对应的类型我都想要。
 *
 * type MyTuple = ['tesla', 'model 3', 'model X'];
 *
 * // 意思就是:MyTuple 这个数组里,所有可能存在的值的类型集合
 * type All = MyTuple[number];
 *
 * // 结果:All = 'tesla' | 'model 3' | 'model X'
 */

Solution by GuoYingHAOG #37734

type TupleObject<T extends readonly PropertyKey[]> = {
    [P in T[number]]: P
}

Solution by GuoYingHAOG #37725

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

Solution by ortima #37691

type TupleToObject<T extends readonly (string | number | symbol)[]> = {
  [P in T[number]]: P
}

/* _____________ 测试用例 _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const
const tupleNumber = [1, 2, 3, 4] as const
const sym1 = Symbol(1)
const sym2 = Symbol(2)
const tupleSymbol = [sym1, sym2] as const
const tupleMix = [1, '2', 3, '4', sym1] as const

type cases = [
  Expect<Equal<TupleToObject<typeof tuple>, { 'tesla': 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y' }>>,
  Expect<Equal<TupleToObject<typeof tupleNumber>, { 1: 1, 2: 2, 3: 3, 4: 4 }>>,
  Expect<Equal<TupleToObject<typeof tupleSymbol>, { [sym1]: typeof sym1, [sym2]: typeof sym2 }>>,
  Expect<Equal<TupleToObject<typeof tupleMix>, { 1: 1, '2': '2', 3: 3, '4': '4', [sym1]: typeof sym1 }>>,
]

// @ts-expect-error
type error = TupleToObject<[[1, 2], {}]>

Solution by PosionEdgar #37677

type TupleToObject<T extends readonly (string | number | symbol)[]> = {
  [P in T[number]]: P;
}

Solution by AlexanderNP #37644

type TupleToObject<T extends readonly (string | number | symbol)[]>={
   [Key in T[number]]: Key
}

Solution by BernTheSeal #37631

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

Solution by jjojae9393 #37610

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

Solution by tomo-local #37574