test(ts-toolbelt): recursive iteration types (#33810)

* test: recursive iteration types for ts-toolbelt

* fix: implementation details

* fix: comment

* Update index.ts
This commit is contained in:
Pierre-Antoine Mills 2019-12-05 19:47:25 +02:00 committed by Nathan Shively-Sanders
parent 09271f107d
commit 9a766c3197
3 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,44 @@
// ! this library is mostly used with ramda
import {I, T, Test} from "ts-toolbelt";
const {check, checks} = Test;
// iterates over `T` and returns the `Iteration` position when finished
type StdRecursiveIteration<T extends any[], I extends I.Iteration = I.IterationOf<'0'>> = {
0: StdRecursiveIteration<T, I.Next<I>>;
1: I.Pos<I>;
}[
I.Pos<I> extends T.Length<T> // this form of recursion is preferred
? 1 // because it will let the user know if
: 0 // the instantiation depth has been hit
]; // (but error is sometimes swallowed (?))
checks([
check<StdRecursiveIteration<[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]>, 40, Test.Pass>(), // max length is 40
]);
// iterates over `T` and returns the `Iteration` position when finished
type SafeRecursiveIteration<T extends any[], I extends I.Iteration = I.IterationOf<'0'>> = {
0: SafeRecursiveIteration<T, I.Next<I>>;
1: I.Pos<I>;
}[
I.Key<I> extends T.Length<T, 's'> // this form of recursion is the safest
? 1 // because `T.Length<T, 's'>` will force
: 0 // the length to comply with the limits
]; // => won't compute if excessive length
checks([
check<SafeRecursiveIteration<[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]>, 0, Test.Pass>() // did not compute
]);

View File

@ -0,0 +1,14 @@
{
"name": "ts-toolbelt-test",
"version": "1.0.0",
"description": "",
"author": "",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "https://github.com/pirix-gh/ts-toolbelt"
},
"dependencies": {
"ts-toolbelt": "latest"
}
}

View File

@ -0,0 +1,5 @@
{
"compilerOptions": {
"strict": true
}
}