-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathindex.d.ts
33 lines (32 loc) · 1.18 KB
/
index.d.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
import { Dec, Concat, Unshift, Cast } from '..';
// A mathematical game or puzzle that consists of three rods and a number of disks of different sizes:
// https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/uncategorized/hanoi-tower.
//
type S = Hanoi<2>; // [["a", "c"], ["a", "b"], ["c", "b"]]
//
// This type uses recursive (and not officially supported) type alias, see more:
// https://github.com/microsoft/TypeScript/issues/26223#issuecomment-513187373.
export type Hanoi<
// The number of discs to use.
N extends number,
// The first rod.
A = 'a',
// The second rod.
B = 'b',
// The third rod.
C = 'c'
> = {
//
finish: [];
//
//
// Computation is split into multiple steps with `infer`, see more:
// https://github.com/pirix-gh/medium/blob/master/types-curry-ramda/src/index.ts#L17.
next: Hanoi<Dec<N>, C, B, A> extends infer H // Assign result to `H`
? Unshift<Cast<H, Array<any>>, [A, B]> extends infer G // Assign result to `G`
? Hanoi<Dec<N>, A, C, B> extends infer F // Assign result to `F`
? Concat<Cast<F, Array<any>>, Cast<G, Array<any>>>
: never
: never
: never;
}[N extends 0 ? 'finish' : 'next'];