-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshift-by.ts
36 lines (34 loc) · 1.08 KB
/
shift-by.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import type { Equal, ExpectTrue } from '@type-challenges/utils'
import type { Subtract } from '../math/subtract'
import type { UnknownArray } from '../unknown-array'
/**
* Removes the first `N` elements from a tuple type. Does not return the removed types.
*
* @remarks Its worth noting `Pop`, `Push`, `Shift`, and `Unshift` are inspired by JS; however, the types are not 1:1 for design reasons. More information can be found in [README](
* @example
* ```
* declare const tuple: ['d', 'a', 'v', 'i', 'd']
* type Tuple = ShiftN<typeof tuple, 2> // ['v', 'i', 'd']
* ```
*/
export type ShiftBy<T extends UnknownArray, N extends number> = T extends [
any,
...infer U
]
? N extends 0
? T
: ShiftBy<U, Subtract<N, 1>>
: T extends readonly [any, ...infer U]
? N extends 0
? T
: ShiftBy<U, Subtract<N, 1>>
: T
/**
* @internal
*/
type _Cases = [
ExpectTrue<Equal<ShiftBy<['a', 'b', 'c'], 0>, ['a', 'b', 'c']>>,
ExpectTrue<Equal<ShiftBy<['a', 'b', 'c'], 1>, ['b', 'c']>>,
ExpectTrue<Equal<ShiftBy<['a', 'b', 'c'], 2>, ['c']>>,
ExpectTrue<Equal<ShiftBy<['a', 'b', 'c'], 3>, []>>
]