mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-04 21:53:42 -06:00
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:
parent
09271f107d
commit
9a766c3197
44
tests/cases/user/ts-toolbelt/index.ts
Normal file
44
tests/cases/user/ts-toolbelt/index.ts
Normal 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
|
||||
]);
|
||||
14
tests/cases/user/ts-toolbelt/package.json
Normal file
14
tests/cases/user/ts-toolbelt/package.json
Normal 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"
|
||||
}
|
||||
}
|
||||
5
tests/cases/user/ts-toolbelt/tsconfig.json
Normal file
5
tests/cases/user/ts-toolbelt/tsconfig.json
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"strict": true
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user