09896-medium-get-middle-element

Back

type GetMiddleElement<T extends any[]> = T['length'] extends 1 | 2
  ? T
  : T extends [any, ...infer R, any]
  ? GetMiddleElement<R>
  : []

Solution by Keith-Web3 #35197

type GetMiddleElement<T extends any[]> =
  T['length'] extends 0 | 1 | 2
  ? T
    : T extends [any, ...infer M, any]
    ? GetMiddleElement<M>
  : never

Solution by wendao-liu #35097

type GetMiddleElement<T extends any[], L = T['length']> = L extends 0
  ? []
  : L extends 1 | 2
  ? T
  : T extends [infer First, ...infer M, infer Last]
  ? GetMiddleElement<M>
  : never

Solution by duodou #34591

type GetMiddleElement<T> = T extends [any, ...infer R, any]
  ? R extends []
    ? T
    : GetMiddleElement<R>
  : T

Solution by MAXLZ1 #34304

type Len = 0 | 1 | 2
type GetMiddleElement<T extends any[]> = T['length'] extends Len ? T : T extends [any, ...infer rest , any] ? GetMiddleElement<rest> :  T

Solution by ouzexi #34113

9896 - GetMiddleElement

by 凤之兮原 (@kongmingLatern) #medium

Question

Get the middle element of the array by implementing a GetMiddleElement method, represented by an array

If the length of the array is odd, return the middle element If the length of the array is even, return the middle two elements

For example

  type simple1 = GetMiddleElement<[1, 2, 3, 4, 5]>, // expected to be [3]
  type simple2 = GetMiddleElement<[1, 2, 3, 4, 5, 6]> // expected to be [3, 4]

View on GitHub: https://tsch.js.org/9896

...

type GetMiddleElement<T extends unknown[], L extends unknown[] = [], R extends unknown[] = []> =
  T['length'] extends 0|1|2
    ? T
    : T extends [infer _, ...infer M, infer _]
      ? GetMiddleElement<M>
      : never

Solution by veralex #33840

type RemoveLeft<T> = T extends [infer _, ...infer End] ? End : never

type RemoveRight<T> = T extends [...infer Start, infer _] ? Start :never

type GetMiddleElement<T extends any[]> = T['length'] extends 0 |1 | 2 ? T : GetMiddleElement<RemoveLeft<RemoveRight<T>>>

Solution by PiligrimStas #33800

解法1 - 暴力:

type GetMiddleElement<T extends any[], L extends any[] = [], R extends any[] = [], M extends "L" | "R" = "L"> = T["length"] extends 0
  ? []
  : [...L, ...R]["length"] extends T["length"]
  ? L["length"] extends R["length"]
    ? [[1, ...T][L["length"]], [1, ...T][[...L, 1]["length"]]]
    : [[1, ...T][L["length"]]]
  : M extends "L"
  ? GetMiddleElement<T, [...L, 1], R, "R">
  : GetMiddleElement<T, L, [...R, 1], "L">;

解法2 - 类似动态规划的思路:

type GetMiddleElement<T extends any[]> = T extends 0 | 1 | 2 ? T : T extends [any, infer M, any] ? GetMiddleElement<M> : [];

Solution by tototi5997 #33547

type GetMiddleElement<T extends any[]> = T['length'] extends 0 | 1 | 2
  ? T
  : T extends [infer _, ...infer Rest, infer _]
  ? GetMiddleElement<Rest>
  : never;

Solution by sunupupup #33446

// your answers
type GetMiddleElement<T> = T extends [infer S, ...infer M, infer L]
? M['length'] extends 1
? M : M['length'] extends 0 
? [S,L] : GetMiddleElement<M>
:T

Solution by pea-sys #33270

// 你的答案

type GetMiddleElement<T extends any[]> = T extends [infer L,...infer M , infer R] 
                                                  ? M extends [] 
                                                      ? [L,R]
                                                      : GetMiddleElement<M>
                                                  : T

Solution by shanguoteng #32816

type GetMiddleElement<T extends unknown[]> =  T extends [infer First, ...infer Middle, infer Last]
  ? Middle['length'] extends 1
    ? Middle
    : Middle['length'] extends 0
      ? [First, Last]
      : GetMiddleElement<[...Middle]>
  : T```

Solution by keyurparalkar #32709

// your answers
type GetMiddleElement<T extends any[]> = T extends [] 
  ? [] 
  : T extends [infer Last] 
    ? T 
    : T extends [infer First, infer Last] 
      ? T 
      : T extends [infer First, ...infer Middles extends any[], infer Last]
        ? GetMiddleElement<Middles> 
        : never;

Solution by kakasoo #32163

type GetMiddleElement<T extends any[], U = T> = T extends [
  infer _F,
  ...infer C,
  infer _L
]
  ? C["length"] extends 1 | 2
    ? C
    : GetMiddleElement<C, T>
  : U;

Solution by moonpoet #31021

type GetMiddleElement<T extends any[]> = T extends [infer X, ...infer Y, infer Z] ?
  Y extends [] ? [X, Z] : GetMiddleElement<Y>
  : T

Solution by zzz1220 #30880

// your answers
type GetMiddleElement<T extends unknown[]> =
  T extends [infer F, ...infer M, infer L]
    ? M extends []
      ? [F, L]
      : [M] extends [never]
        ? F
        : GetMiddleElement<M>
    : T

Solution by enochjs #30542

// 你的答案
type GetMiddleElement<T> = T extends [unknown, ...infer R, unknown] ? R extends [] ? T : GetMiddleElement<R> : T

Solution by YE840926098 #30136

type Shift<T> = T extends [any, ...infer Rest] ? Rest : never;

type GetMiddle<T extends unknown[], K extends unknown[]> = T extends [
  infer First,
  infer Second,
  ...any
]
  ? K["length"] extends 0
    ? [First, Second]
    : [First]
  : T;

type GetMiddleElement<
  T extends unknown[],
  Fast extends unknown[] = T,
  Slow extends unknown[] = T
> = Fast extends [any, any, ...infer Rest]
  ? GetMiddleElement<T, Rest, [] extends Rest ? Slow : Shift<Slow>>
  : GetMiddle<Slow, Fast>;

Solution by idebbarh #29942

// 提取构造 // 当 T 长度为 0 | 1 | 2 时,中间元素就是 T 本身 // 然后构造,不断递归,去掉数组第一个和最后一个,提取中间元素,直至满足上面条件

type GetMiddleElement<T extends unknown[]> =
  T['length'] extends 0 | 1 | 2
  ? T
  : T extends [infer F, ...infer M, infer L]
    ? GetMiddleElement<M>
    : never

Solution by stevenaces #29450

type GetMiddleElement<T extends unknown[]> = T extends
  | [infer Midlle]
  | [infer Left, infer Right]
  ? T
  : T extends [infer First, ...infer Rest, infer Last]
  ? GetMiddleElement<Rest>
  : T;

Solution by DoubleWoodLin #28852

type GetMiddleElement<T extends unknown[]> =
  T["length"] extends 0 | 1 | 2 ?
  T :
  T extends [infer _, ...infer Rest, infer _]
    ? GetMiddleElement<Rest>
    : []

Solution by smileboyi #27218

type PlusOne<Number, Current extends any[]= [], Next extends any[] = [...Current, never]> = Number extends Current['length'] ? Next['length'] : PlusOne<Number, Next>
type GetMiddleElement<T extends readonly any[]> = GetMiddleHelper<T>

type GetMiddleHelper<T extends readonly any[], Prev extends 'hi'[] = []> = 
  T extends [infer First, ...infer Rest] 
    ? Rest['length'] extends Prev['length'] 
      ? [First]
      : PlusOne<Prev['length']> extends Rest['length'] ? [First, Rest[0]]: GetMiddleHelper<Rest, ['hi', ...Prev]> 
    : [] // Base Case

Solution by amsuarez99 #26774

type GetMiddleElement<TArray extends any[]> = TArray extends [
  infer Head,
  ...infer Center,
  infer Tail,
]
  ? Center extends []
    ? [Head, Tail]
    : GetMiddleElement<Center>
  : TArray

Solution by valentynpodkradylin #26270

// your answers
type GetMiddleElement<T extends any[]> = T extends [
  infer F,
  ...infer M,
  infer L
]
  ? M["length"] extends 0
    ? [F, L]
    : GetMiddleElement<M>
  : T;

Solution by DvYinPo #25904

// 你的答案
type GetMiddleElement<T extends unknown[]> = 
  T['length'] extends 0 | 1 | 2
  ? T 
  : T extends [infer X, ...infer M, infer Y] 
    ? GetMiddleElement<M>
    : []

Solution by kiki-zjq #25765

type Head<T> = T extends [infer First, ...any[]]
  ? First
  : never
type Tail<T> = T extends [any, ...infer Rest]
  ? Rest
  : []
type Helper<Fast extends unknown[], Slow> = Fast extends [any, any, ...infer Rest]
  ? Helper<Rest, Tail<Slow>>
  : Fast['length'] extends 1
    ? [Head<Tail<Slow>>]
    : [Head<Slow>, Head<Tail<Slow>>]
type GetMiddleElement<T extends unknown[]> = T['length'] extends 0
  ? []
  : Helper<T, [never, ...T]>

Solution by Sun79 #24895

type GetMiddleElement<T extends any[]> = T['length'] extends 0 | 1 | 2
  ? T
  : T extends [infer _, ...infer R, infer __]
  ? GetMiddleElement<R>
  : never;

Solution by JohnLi1999 #24688

// your answers
type GetMiddleElement<T extends unknown[]> = T extends [infer F,...infer M, infer L]
                                              ? M extends []
                                                ? [F,L]
                                                : GetMiddleElement<M>
                                              : T

Solution by studymachiney #24682

type GetMiddleElement<T extends any[]> = T extends [any, ...infer R, any] ? R extends [] ? T : GetMiddleElement<R> : T;

// old way
// type GetMiddleElement<T extends any[]> = T extends [] | [any] | [any, any] ? T :
//   (T extends [any, ...infer R] ?    //[->,...]
//     (R extends [...infer RR, any] ? GetMiddleElement<RR> : T) :   //[...,<-]
//     T);

Solution by E-uler #24518

// your answers
type GetMiddleElement<T extends unknown[]> = T extends [infer F,...infer M, infer L] ?
 M extends [] ?[F,L] : GetMiddleElement<M> : T

Solution by snakeUni #23330