From f0b67b59fdbc2bcebd60f311af716c617724022e Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Mills Date: Fri, 4 Oct 2019 16:10:25 +0200 Subject: [PATCH 1/4] test: recursive iteration types for ts-toolbelt --- tests/cases/user/ts-toolbelt/index.ts | 29 ++++++++++++++++++++++ tests/cases/user/ts-toolbelt/package.json | 14 +++++++++++ tests/cases/user/ts-toolbelt/tsconfig.json | 5 ++++ 3 files changed, 48 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 0000000000000..7b20589ad66fd --- /dev/null +++ b/tests/cases/user/ts-toolbelt/index.ts @@ -0,0 +1,29 @@ +import {I, T, Test} from "ts-toolbelt"; + +const {check, checks} = Test; + +// iterates over `T` and returns the `Iteration` position when finished +type RecursiveIteration> = { + 0: RecursiveIteration>; + 1: I.Pos; +}[ + T.Length extends I.Pos + ? 1 + : 0 +]; + +checks([ + check, 40, Test.Pass>(), // max length is 40 + check, number, Test.Pass>() // it overflowed +]); diff --git a/tests/cases/user/ts-toolbelt/package.json b/tests/cases/user/ts-toolbelt/package.json new file mode 100644 index 0000000000000..d066aeec2d688 --- /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 0000000000000..fc0c19189c360 --- /dev/null +++ b/tests/cases/user/ts-toolbelt/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "strict": true + } +} From 2ace88eb123844fef25772253c03383ff98754fa Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Mills Date: Sat, 5 Oct 2019 14:04:22 +0200 Subject: [PATCH 2/4] fix: implementation details --- tests/cases/user/ts-toolbelt/index.ts | 31 +++++++++++++++++++-------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/tests/cases/user/ts-toolbelt/index.ts b/tests/cases/user/ts-toolbelt/index.ts index 7b20589ad66fd..8f71a933bbc9d 100644 --- a/tests/cases/user/ts-toolbelt/index.ts +++ b/tests/cases/user/ts-toolbelt/index.ts @@ -3,27 +3,40 @@ import {I, T, Test} from "ts-toolbelt"; const {check, checks} = Test; // iterates over `T` and returns the `Iteration` position when finished -type RecursiveIteration> = { - 0: RecursiveIteration>; +type StdRecursiveIteration> = { + 0: StdRecursiveIteration>; 1: I.Pos; }[ - T.Length extends I.Pos - ? 1 - : 0 -]; + 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 swallowed by type atm) checks([ - check, 40, Test.Pass>(), // max length is 40 - check> = { + 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, number, Test.Pass>() // it overflowed + ]>, 0, Test.Pass>() // did not compute ]); From 2bdd6a9760514f2e3af5069849f60b5876e8b3a5 Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Mills Date: Sat, 5 Oct 2019 14:05:42 +0200 Subject: [PATCH 3/4] fix: comment --- tests/cases/user/ts-toolbelt/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cases/user/ts-toolbelt/index.ts b/tests/cases/user/ts-toolbelt/index.ts index 8f71a933bbc9d..054d707911178 100644 --- a/tests/cases/user/ts-toolbelt/index.ts +++ b/tests/cases/user/ts-toolbelt/index.ts @@ -10,7 +10,7 @@ type StdRecursiveIteration 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 swallowed by type atm) +]; // (but error is sometimes swallowed (?)) checks([ check Date: Wed, 16 Oct 2019 01:47:32 +0200 Subject: [PATCH 4/4] Update index.ts --- tests/cases/user/ts-toolbelt/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/cases/user/ts-toolbelt/index.ts b/tests/cases/user/ts-toolbelt/index.ts index 054d707911178..218db6ba38dc0 100644 --- a/tests/cases/user/ts-toolbelt/index.ts +++ b/tests/cases/user/ts-toolbelt/index.ts @@ -1,3 +1,5 @@ +// ! this library is mostly used with ramda + import {I, T, Test} from "ts-toolbelt"; const {check, checks} = Test;