00010-medium-tuple-to-union

Back

type TupleToUnion<T extends readonly any[]> = T[number]

Solution by HrOkiG2 #35214

// 你的答案
type TupleToUnion<T extends any[]> = T[number]

Solution by HoseaGuo #35161

type TupleToUnion<T extends unknown[]> = T extends never 
  ? never 
  : T extends [infer First, ...infer Rest] 
    ? First | TupleToUnion<Rest> 
    : never

Solution by eunsukimme #35120

type TupleToUnion<T extends any[]> = T[number];

Solution by raeyoung-kim #34999

// 你的答案

Solution by lizncoder #34996

type TupleToUnion<T extends any[]> = T[number];

Solution by 56aiden90 #34921

type TupleToUnion<T extends Array<unknown>> = T['length'] extends 0 ? never : T[number]

Solution by linkinzhiyuan #34908

type TupleToUnion<T extends readonly any[]> = T[number]

Solution by lfz9527 #34830

Just extend T's type in Array. Then we can define the type in typeof T's elements.


type TupleToUnion<T extends any[]> = T[number];

Solution by dev-jaemin #34632

type TupleToUnion<T extends unknown[]> = T[number]

Solution by binhdv155127 #34622

type TupleToUnion<T extends any[]> = T extends (infer I)[] ? I : never
// your answers

Solution by Rustamaha #34596

type TupleToUnion<T extends any[]> = T[number]

Solution by devshinthant #34563

type TupleToUnion<T extends any[]> = T[number];

T[number]使用了索引访问类型(Indexed Access Types),它表示从类型T中通过数字索引能够访问到的所有属性的类型,对于数组或元组来说,这意味着它所有的类型。

Solution by wxh-cyber #34469

type TupleToUnion<T> = T extends [infer F, ...infer R] ? F | TupleToUnion<R> : never;

Solution by ktim816 #34433

type TupleToUnion<T extends unknown[]> = T[number];

Solution by ProvorovOleksii #34278

// 你的答案

type TupleToUnion = T extends (infer A)[] ? A : never

Solution by W-fitTiger #34254

type TupleToUnion<T extends any[]> = keyof { [k in T[number]]: boolean }

Solution by quitone #34204

문제설명

튜플 값으로 유니온 타입을 생성하는 제네릭 TupleToUnion<T>를 구현하세요.

예시:

type Arr = ["1", "2", "3"];

type Test = TupleToUnion<Arr>; // expected to be '1' | '2' | '3'

풀이

마찬가지로 크게 어려운 문제는 아니었습니다. Tuple to object 문제와 유사하게 문제를 풀이하면 됩니다.

Solution

/* _____________ 여기에 코드 입력 _____________ */

type TupleToUnion<T extends any[]> = T[number];

/* _____________ 테스트 케이스 _____________ */
import type { Equal, Expect } from "@type-challenges/utils";

type cases = [
	Expect<Equal<TupleToUnion<[123, "456", true]>, 123 | "456" | true>>,
	Expect<Equal<TupleToUnion<[123]>, 123>>
];

Solution by adultlee #34134

// 你的答案
type TupleToUnion<T extends any[]> = T[number];

Solution by yyl134934 #34082

type TupleToUnion<T> = T extends Array<infer P> ? P : never

Solution by ouzexi #33987

type TupleToUnion<T extends unknown[]> = T extends [infer First, ...infer Rest] 
  ? First | TupleToUnion<Rest> 
  : never;

Solution by notsecret32 #33889

type TupleToUnion<T> = T extends Array<infer P>
  ? P
  : never

Solution by Danny101201 #33809

type TupleToUnion<T> = T extends Array<infer A> ? A : never

Solution by laplace1009 #33715

type TupleToUnion<T extends any[]> = T[number]

Solution by rookiewxy #33657

type TupleToUnion<T extends any[]> = T[number]

Solution by rookiewxy #33656

type TupleToUnion<T extends Array<any>> = T[number];

Solution by nupthale #33578

type TupleToUnion<T> = T[any]

Solution by Kolufs #33538

// 你的答案
type TupleToUnion<T extends any[]> = T[number];

Solution by 2531800823 #33248

type TupleToUnion<T extends any[]> = T extends (infer Q)[] ? Q :never;

Solution by RanungPark #33106

// 你的答案
type TupleToUnion<T extends any[]> = keyof {
  [K in T[number]]: K
}

Solution by DwayneDuanJY #33073