Skip to content

Commit 9a766c3

Browse files
millspsandersn
authored andcommitted
test(ts-toolbelt): recursive iteration types (#33810)
* test: recursive iteration types for ts-toolbelt * fix: implementation details * fix: comment * Update index.ts
1 parent 09271f1 commit 9a766c3

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

tests/cases/user/ts-toolbelt/index.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// ! this library is mostly used with ramda
2+
3+
import {I, T, Test} from "ts-toolbelt";
4+
5+
const {check, checks} = Test;
6+
7+
// iterates over `T` and returns the `Iteration` position when finished
8+
type StdRecursiveIteration<T extends any[], I extends I.Iteration = I.IterationOf<'0'>> = {
9+
0: StdRecursiveIteration<T, I.Next<I>>;
10+
1: I.Pos<I>;
11+
}[
12+
I.Pos<I> extends T.Length<T> // this form of recursion is preferred
13+
? 1 // because it will let the user know if
14+
: 0 // the instantiation depth has been hit
15+
]; // (but error is sometimes swallowed (?))
16+
17+
checks([
18+
check<StdRecursiveIteration<[
19+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
21+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
22+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
23+
]>, 40, Test.Pass>(), // max length is 40
24+
]);
25+
26+
// iterates over `T` and returns the `Iteration` position when finished
27+
type SafeRecursiveIteration<T extends any[], I extends I.Iteration = I.IterationOf<'0'>> = {
28+
0: SafeRecursiveIteration<T, I.Next<I>>;
29+
1: I.Pos<I>;
30+
}[
31+
I.Key<I> extends T.Length<T, 's'> // this form of recursion is the safest
32+
? 1 // because `T.Length<T, 's'>` will force
33+
: 0 // the length to comply with the limits
34+
]; // => won't compute if excessive length
35+
36+
checks([
37+
check<SafeRecursiveIteration<[
38+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
39+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
40+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
41+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
42+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43+
]>, 0, Test.Pass>() // did not compute
44+
]);
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "ts-toolbelt-test",
3+
"version": "1.0.0",
4+
"description": "",
5+
"author": "",
6+
"license": "Apache-2.0",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/pirix-gh/ts-toolbelt"
10+
},
11+
"dependencies": {
12+
"ts-toolbelt": "latest"
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"strict": true
4+
}
5+
}

0 commit comments

Comments
 (0)