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;
};
PropertyKey
type is string|number|symbol
.{ key in T[number] }: key
creates an object where, for each element in T, it generates an object with the element value as both the key and the value."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
type Values<T extends readonly any[]> = T[number];
let a :Values<[1,2,3,4]>
type TupleToObject<T extends readonly any[]> = {
[Value in Values<T>] : Value;
}
Solution by bkdragon0228 #34240
type TupleToObject<T extends readonly any[]> = {
[K in T[number]]: K
}
const brands = ['samsung', 'xiomi', 'apple'] as const
type brandObj = TupleToObject<typeof brands>
Solution by Fmanuel809 #34185
type TupleToObject<T extends readonly (keyof any)[]> = {
[k in T[number]]: k
}
Solution by quitone #34181
// ๋งต๋ ํ์
์ ์ด์ฉํด ์๋ก์ด ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
type TupleToObject<T extends readonly any[]> = {
// T์ ๋ชจ๋ ์์๋ฅผ ํ๋์ฉ ์ํํด์ K๋ฅผ key๊ฐ์ผ๋ก ๊ฐ์ง๋ ๊ฐ์ฒด๋ฅผ ์์ฑ
[K in T[number]]: K;
}
Solution by leeeunji93 #34032
type TupleToObject<T extends readonly any[]> = {
[K in T[number]]: K;
}
Solution by leeeunji93 #34031