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
// your answers
type TupleToObject<T extends readonly (string | number | symbol)[]> = { [P in T[number]]:P }
Solution by PAVANT009 #37559
// 你的答案
## ✅ 正确实现
```ts
type TupleToObject<T extends readonly (string | number | symbol)[]> = {
[P in T[number]]: P
}
T extends readonly (string | number | symbol)[]
为什么需要这个约束?
string | number | symbol 类型readonly 确保处理的是字面量类型T[number] 获取元素值的联合类型// 对于 tuple = ['tesla', 'model 3'] as const
type Elements = T[number] // "tesla" | "model 3"
T[number] 是索引访问类型,它获取数组中所有元素的联合类型。
[P in T[number]]: P
//这相当于:
// 遍历联合类型 "tesla" | "model 3"
{
tesla: "tesla";
"model 3": "model 3";
}
完整正确代码
/* _____________ 你的代码 _____________ */
type TupleToObject<T extends readonly (string | number | symbol)[]> = {
[P in T[number]]: P
}
keyof T - 获取键的集合T 的所有属性名Object.keys() 的类型版本T[number] - 获取值的集合T 的所有元素值类型map() 操作的类型版本// 使用 keyof - 基于属性名创建新类型
type Getters<T> = {
[K in keyof T]: () => T[K]
}
interface User {
name: string;
age: number;
}
type UserGetters = Getters<User>;
// { name: () => string; age: () => number }
// 使用 T[number] - 基于元素值创建新类型
type TupleToObject<T extends readonly PropertyKey[]> = {
[P in T[number]]: P
}
const colors = ['red', 'green', 'blue'] as const;
type ColorMap = TupleToObject<typeof colors>;
// { red: "red"; green: "green"; blue: "blue" }
keyof → 想到 Keys(钥匙/键)→ 获取键名
T[number] → 想到数组索引访问 → 获取值类型
Solution by nyk233233 #37430
type TupleToObject<T extends readonly any[]> = {
[I in T[number]] : I
}
T extends readonly any[]T must be a tuple (or readonly array).
Example:
const tuple = ['a', 'b', 'c'] as const;
type T = typeof tuple;
// readonly ["a", "b", "c"]
T[number]In TypeScript, indexing a tuple/array type with number gives you the union of all its element types.
Example:
type Elements = ["a", "b", "c"][number];
// "a" | "b" | "c"
So if T = readonly ["a", "b", "c"], then T[number] = "a" | "b" | "c".
[I in T[number]]I in the union "a" | "b" | "c", create a property whose key is I.: II itself."a" maps to "a", "b" maps to "b", etc.If you pass in:
const tuple = ["a", "b", "c"] as const;
type Result = TupleToObject<typeof tuple>;
Then:
type Result = {
a: "a";
b: "b";
c: "c";
}
✅ In plain English: My type takes a tuple of literal values and produces an object type where each element of the tuple becomes both a key and its corresponding value.
Solution by 3aluw #37421
type TupleToObject<T extends readonly any[]> = {
[D in T[number]]: D;
}
Solution by minseonkkim #37346
type TupleToObject<T extends readonly any[]> = {
[P in T[number]]: P;
};
Solution by Nakamura25257 #37256
type TupleToObject<T extends readonly PropertyKey[]> = {
[P in T[number]]: P;
};
type TupleToObject<T extends readonly (keyof any)[]> = {
[P in T[number]]: P;
};
type TupleToObject<T extends readonly (string|symbol|number)[]> = {
[P in T[number]]: P;
};
三种写法
T 是一个元组类型,T[number] 表示元组中所有项的 | 联合类型key,要对元素项做类型限制keyof any 可以得到对象 key 所有可取的类型 (string|number|symbol)Solution by djdidi #37118
type TupleToObject<T extends readonly (string | number | symbol)[]> = {
[P in T[number]]:P
}
Solution by 359Steve #37012
type TupleToObject<T extends readonly any[]> = {
[P in T[number]]: P;
}
Solution by mag123c #36993
// readonly 对应 as const
// T[number],把数组所有元素组成的联合类型
type TupleToObject<T extends readonly string[]> = {
[P in T[number]]: P
}
Solution by YiShengYouNi #36976
//here PropertyKey = (string | number | symbol)
type TupleToObject<T extends readonly PropertyKey[]> = {
[a in T[number]]:a
}
So, T is an array that takes number as input. <T extends readonly any[]> was a problem . any[] changed to PropertyKey[].
Solution by Anonymous961 #36959