Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions tests/cases/user/ts-toolbelt/index.ts
Original file line number Diff line number Diff line change
@@ -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
]);
14 changes: 14 additions & 0 deletions tests/cases/user/ts-toolbelt/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
5 changes: 5 additions & 0 deletions tests/cases/user/ts-toolbelt/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"strict": true
}
}