00011-easy-tuple-to-object

Back

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

Solution by thukyaw11 #35526

type TupleToObject<T extends ReadonlyArray<string | number | symbol>> = { [ key in T[number]]: key };

Solution by gangnamssal #35467

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

// ไฝ ็š„็ญ”ๆกˆ

Solution by dixiaioping #35383

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

answar

Solution by SB-SLIM #35376

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

Solution by gyeounjeong #35351

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

Solution by Muntazir-sd #35211

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

Solution by wendao-liu #35175

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

Solution by wendao-liu #35146

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

Solution by miju-Park #34963

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

Solution by raeyoung-kim #34926

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

Solution by jsk3342 #34899

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

Solution by raeyoung-kim #34895

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

Solution by 56aiden90 #34867

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

Solution by yuri-suhomlinov-stenn #34861

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

Solution by nguyste #34852

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

Solution by eunsukimme #34808

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

Solution by kang-kibong #34742

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

T ๆ˜ฏไธ€ไธชๅช่ฏป็š„ ๅ…ƒ็ป„ T[number]ไธบๅ…ƒ็ป„ไธญๆ‰€ๆœ‰ๅ…ƒ็ด ็ฑปๅž‹็ป„ๆˆ็š„่”ๅˆ็ฑปๅž‹

Solution by oOzhuoxiaOo #34674

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

Solution by zeyuanHong0 #34667

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

Solution by nathan2slime #34654

/* _____________ Your Code Here _____________ */

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

/* _____________ 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 }>>,
]

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

/* _____________ Further Steps _____________ */
/*
  > Share your solutions: https://tsch.js.org/11/answer
  > View solutions: https://tsch.js.org/11/solutions
  > More Challenges: https://tsch.js.org
*/

Solution by dengsflyalway #34595

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

Solution by devshinthant #34539

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

Solution by JayoonKoo #34515

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

Solution by gobielJonathan #34448

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

Solution by ktim816 #34419

// ไฝ ็š„็ญ”ๆกˆ
type TupleToObject<T extends readonly PropertyKey[]> = {
  [k in T[number]]: k
}

่งฃๆž๏ผš
PropertyKey๏ผš TS็š„ๅ†…็ฝฎ็ฑปๅž‹๏ผŒ็ญ‰ไบŽstring | number | symbol
T[number]๏ผš ็ดขๅผ•็ฑปๅž‹๏ผŒไธป่ฆ่Žทๅ–ๅˆฐๆ•ฐ็ป„/ๅ…ƒ็ฅ–็ฑปๅž‹ๅฏ็”จๆ•ฐๅญ—็ดขๅผ•่Žทๅ–ๅˆฐ็š„่”ๅˆ็ฑปๅž‹

Solution by ICE-XIAOWU #34382

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

Solution by souzaramon #34320

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

or
//type PropertyKey = string | number | symbol;  
// a built-in PropertyKey type which is string | number | symbol
type TupleToObject<T extends readonly PropertyKey[]> = {
   [P in T[number]]: P
}

Solution by binhdv155127 #34299

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

Solution by notcodev #34290

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

Solution by wxh-cyber #34279