21106-medium-zu-he-jian-lei-xing-combination-key-type

Back

type Combs<T extends any[]> = T extends [infer F extends string, ...infer Rest extends any[]]
  ? `${F} ${Rest[number]}` | Combs<Rest>
  : never;

Solution by wendao-liu #35126

type Combs<T extends any[]> = T extends [infer R, ...infer rest] ? `${R & string} ${rest[number] & string}` | Combs<rest> : never

Solution by ouzexi #34147

// your answers
type Combs<T extends any[]> = T extends [infer L extends string, ...infer R extends string[]]
 ? `${L} ${R[number]}` | Combs<R>
  : never

Solution by pea-sys #33543

type Combs<T extends string[]> = T['length'] extends 0 | 1
  ? never
  : T extends [infer First, ...infer Rest extends string[]]
  ? `${First & string} ${Rest[number]}` | Combs<Rest>
  : never;

Solution by sunupupup #33479

type Combs<T extends any[], Ret = never> = 
  T extends [infer F extends string, ...infer R extends string[]] 
    ? Combs<R, Ret | `${F} ${R[number]}`>
    : Ret

Solution by bkmashiro #33283

// your answers
type Combs<T extends string[]> = T extends [infer Item extends string, ...infer Rest extends string[]]
  ? `${Item} ${Rest[number]}` | Combs<Rest>
  : never

Solution by DevilTea #33258

type Combs<T extends string[]> =
  T extends [infer First extends string, ...infer Rest extends string[]]
    ? `${First} ${Rest[number]}` | Combs<Rest>
  : never

Playground

Solution by teamchong #33063

type GetUnion<T extends string[], Res extends string = T[0]> = T extends [infer H,  ...infer R]
  ? Res extends H 
    ? GetUnion<string[] & R, Res>
    : `${string & Res} ${string & H}` | GetUnion<string[] & R, Res>
  : never

// 实现 Combs
type Combs<T extends any[]> = T extends [string, ...infer R]
  ? GetUnion<T> | Combs<R>
  : never

Solution by keyurparalkar #32793

// 你的答案

type Combs<T extends string[]> = T extends [infer S extends string, ...infer O extends string[]] ? `${S} ${O[number]}` | Combs<O> : never;

Solution by tarotlwei #31759

// your answers
// 全部列挙するならこれでいいが、重複が入るので今回は NG
// This is fine for listing everything, but it's not suitable for this case because it includes duplicates.
type All<T extends string[], S extends string = T[number]> = S extends S ? `${S} ${Exclude<T[number], S>}` : never


// the answer for this question
type Combs<T extends string[]> = T extends [infer First extends string, ...infer Rest extends string[]] ? `${First} ${Rest[number]}` | Combs<Rest> : never

Solution by Kakeru-Miyazaki #30921

// 你的答案
type Combs<T extends string[]> =
  T extends [infer C extends string, ...infer M extends string[]]
    ? `${C} ${M[number]}` | Combs<M>
    : never

Solution by milletlovemouse #30919

type Combs<T extends string[], R extends unknown[] = []> = T extends [
  infer F extends string,
  ...infer Rest extends string[],
]
  ? Combs<Rest, [...R, `${F} ${Rest[number]}`]>
  : R[number];

Solution by leejaehyup #30876

type Combs<T extends any[]> = T extends [infer A extends string, ...infer B extends any[]] ? `${A} ${B[number]}` | Combs<B> : never

Solution by dreamluo-plus #30695

// your answers
type Combs<T extends string[], RES = ''> = T extends [
  infer HEAD extends string,
  ...infer TAIL extends string[],
]
  ? RES extends ''
    ? Combs<TAIL, `${HEAD} ${TAIL[number]}`>
    : Combs<TAIL, RES | `${HEAD} ${TAIL[number]}`>
  : RES

Solution by playitsafe #30344

type Combs<T extends any[]> = T extends [infer F extends string, ...infer R extends string[]] ? `${F} ${R[number]}` | Combs<R> : never

Solution by sv-98-maxin #30191

type Unshift<T extends any[]> = T extends [infer _,...infer R] ? R: []
type Combs<T extends any[], U extends any[]= Unshift<T>> = 
  T extends [infer A extends string, ...infer E]
    ? E extends [infer B extends string, ...infer R]
      ? `${A} ${B}` | Combs<[A, ...R], U> : Combs<U, Unshift<U>>
    : never

Solution by sv-98-maxin #30190

type Shift<T extends any[]> = T extends [any, ...infer Rest] ? Rest : never;
type Combs<
  T extends any[],
  All extends any[] = Shift<Shift<T>>,
  R extends any = `${T[0]} ${T[1]}`
> = T[0] extends undefined
  ? R
  : All[0] extends undefined
  ? Combs<Shift<T>, Shift<Shift<T>>, R>
  : Combs<T, Shift<All>, R | `${T[0]} ${All[0]}`>;

Solution by idebbarh #30012

// your answers

type Combs<T extends any[]> =  T extends [infer First extends string, ...infer Rest extends any[] ] ? `${First} ${Rest[number]}` | Combs<Rest> : never

Solution by kerolossamir165 #29775

type Combs<T extends any[]> = T extends [
  infer Item extends string,
  ...infer Rest extends string[]
]
  ? `${Item} ${Rest[number]}` | Combs<Rest>
  : never;

Solution by DoubleWoodLin #28896

type Combs<StringArray extends string[]> = StringArray extends [
  infer Head extends string,
  ...infer Tail extends string[]
]
  ? `${Head} ${Tail[number]}` | Combs<Tail>
  : never;

Solution by yevhenpavliuk #28500

这题大概意思就是把数组的每一项和后面的一一组合起来,那么我们可以遍历数组的每一项,利用数组转联合类型,得到结果

type Combs<T extends any[]> = T extends [infer F extends string, ...infer Rest extends string[]] 
  ? `${F} ${Rest[number]}` | Combs<Rest>
  : never

Solution by linjunc #28360

type Combs<
  T extends string[],
  Result extends string = never,
  Key1 extends string = never
> = T extends [infer F extends string, ...infer Rest extends string[]]
  ? [Key1] extends [never]
    ? Combs<Rest, Result, never> | Combs<Rest, Result, F>
    : Combs<Rest, Result | `${Key1} ${F}`, never> | Combs<Rest, Result, Key1>
  : Result;

Solution by alythobani #27621

// It’s recommended to use 'tail recursion'
type Combination<T extends string[], R extends string = never> =
  T extends [infer A extends string, ...infer Rest extends string[]]
    ? Combination<Rest, R | `${A} ${Rest[number]}`>
    : R

type ModifierKeys = ['cmd', 'ctrl', 'opt', 'fn']

type Combs = Combination<ModifierKeys>

Solution by drylint #27401

type ModifierKeys = ['cmd', 'ctrl', 'opt', 'fn']
type Combs<T extends string[] = ModifierKeys> =
  T extends [infer F extends string, ...infer R extends string[]] ? (`${F} ${R[number]}` | Combs<R>) : never

Solution by smileboyi #27275

type Combs<T extends string[], R = never> = T extends [infer F extends string, ...infer Rest extends string[]]
  ? Combs<Rest, R | `${F} ${Rest[number]}`>
  : R

Solution by XkSuperCool #26096

type CombItemAndArray<T extends string, Arr extends string[], Res = never> =
  Arr extends [infer X extends string, ...infer Y extends string[]]
  ? CombItemAndArray<T, Y, Res | `${T} ${X}`>
  : Res 

type Combs<Arr extends string[] = ModifierKeys, Res = never> = 
  Arr extends [infer X extends string, ...infer Y extends string[]]
  ? Combs<Y, Res | CombItemAndArray<X, Y>>
  : Res 

Solution by kiki-zjq #25848

// your answers
type Combs<T extends string[] = []> = T extends [infer Left extends string, ...infer Rest extends string[]] ? `${Left} ${Rest[number]}` | Combs<Rest> : never

Solution by studymachiney #24946

type Combs<
  T extends string[]
> = 
  T extends [infer F extends string,...infer R extends string[] ]?
    `${F} ${R[number]}` | Combs<R>
  :never

Solution by jiangshanmeta #24838

type Combs<T extends string[] = ModifierKeys> = T extends [infer F extends string, ...infer R extends string[]] ? `${F} ${R[number]}` | Combs<R> : never;

Solution by E-uler #24564

type ModifierKeys = ['cmd', 'ctrl', 'opt', 'fn']

// 实现 Combs
type Helper<T> = T extends [infer F extends string, ...infer R extends string[]]
  ? `${F} ${R[number]}` | Helper<R>
  : never
type Combs = Helper<ModifierKeys>

Solution by Sun79 #24496