00011-easy-tuple-to-object

Back

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
}

📝 详细解析

1. 正确的泛型约束

T extends readonly (string | number | symbol)[]

为什么需要这个约束?

2. 使用 T[number] 获取元素值的联合类型

// 对于 tuple = ['tesla', 'model 3'] as const
type Elements = T[number] // "tesla" | "model 3"

T[number] 是索引访问类型,它获取数组中所有元素的联合类型。

3. 映射类型遍历元素值

[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[number] - 获取值的集合

🎯 实际应用场景对比

场景1:对象属性映射

// 使用 keyof - 基于属性名创建新类型
type Getters<T> = {
  [K in keyof T]: () => T[K]
}

interface User {
  name: string;
  age: number;
}

type UserGetters = Getters<User>;
// { name: () => string; age: () => number }

场景2:元组值映射

// 使用 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" }

Solution by nyk233233 #37430

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

1. T extends readonly any[]


2. T[number]


3. [I in T[number]]


4. : I


5. Putting it together

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;
};

三种写法

  1. T 是一个元组类型,T[number] 表示元组中所有项的 | 联合类型
  2. 元组项要作为对象的 key,要对元素项做类型限制
  3. ‼️ 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