From 9a766c31971de538e600b4ce2ba4649d76b79a4c Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Mills Date: Thu, 5 Dec 2019 19:47:25 +0200 Subject: [PATCH] test(ts-toolbelt): recursive iteration types (#33810) * test: recursive iteration types for ts-toolbelt * fix: implementation details * fix: comment * Update index.ts --- tests/cases/user/ts-toolbelt/index.ts | 44 ++++++++++++++++++++++ tests/cases/user/ts-toolbelt/package.json | 14 +++++++ tests/cases/user/ts-toolbelt/tsconfig.json | 5 +++ 3 files changed, 63 insertions(+) create mode 100644 tests/cases/user/ts-toolbelt/index.ts create mode 100644 tests/cases/user/ts-toolbelt/package.json create mode 100644 tests/cases/user/ts-toolbelt/tsconfig.json diff --git a/tests/cases/user/ts-toolbelt/index.ts b/tests/cases/user/ts-toolbelt/index.ts new file mode 100644 index 00000000000..218db6ba38d --- /dev/null +++ b/tests/cases/user/ts-toolbelt/index.ts @@ -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> = { + 0: StdRecursiveIteration>; + 1: I.Pos; +}[ + I.Pos extends T.Length // 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, 40, Test.Pass>(), // max length is 40 +]); + +// iterates over `T` and returns the `Iteration` position when finished +type SafeRecursiveIteration> = { + 0: SafeRecursiveIteration>; + 1: I.Pos; +}[ + I.Key extends T.Length // this form of recursion is the safest + ? 1 // because `T.Length` will force + : 0 // the length to comply with the limits +]; // => won't compute if excessive length + +checks([ + check, 0, Test.Pass>() // did not compute +]); diff --git a/tests/cases/user/ts-toolbelt/package.json b/tests/cases/user/ts-toolbelt/package.json new file mode 100644 index 00000000000..d066aeec2d6 --- /dev/null +++ b/tests/cases/user/ts-toolbelt/package.json @@ -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" + } +} diff --git a/tests/cases/user/ts-toolbelt/tsconfig.json b/tests/cases/user/ts-toolbelt/tsconfig.json new file mode 100644 index 00000000000..fc0c19189c3 --- /dev/null +++ b/tests/cases/user/ts-toolbelt/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "strict": true + } +}