From 35e51b1fc937b1c4198879aab03269a518bc46c2 Mon Sep 17 00:00:00 2001 From: Guga Guichard Date: Fri, 7 Jun 2024 13:41:15 -0300 Subject: [PATCH 1/3] Enforce no-implicit-any in composables declaration --- src/constructors.ts | 4 +++- src/tests/constructors.test.ts | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/constructors.ts b/src/constructors.ts index 66b57044..058d4fb5 100644 --- a/src/constructors.ts +++ b/src/constructors.ts @@ -31,7 +31,9 @@ function toError(maybeError: unknown): Error { * That function is gonna catch any errors and always return a Result. * @param fn a function to be used as a Composable */ -function composable any>(fn: T): Composable { +function composable( + fn: T, +): Composable any>> { return async (...args) => { try { // deno-lint-ignore no-explicit-any diff --git a/src/tests/constructors.test.ts b/src/tests/constructors.test.ts index 14da023e..6a596076 100644 --- a/src/tests/constructors.test.ts +++ b/src/tests/constructors.test.ts @@ -49,6 +49,12 @@ describe('composable', () => { assertEquals(res, success(3)) }) + it('will enforce noImplicitAny', () => { + // @ts-expect-error: implicit any + const fn = composable((a) => a) + type _FN = Expect any>>> + }) + it('infers the types of async functions', async () => { const fn = composable(asyncAdd) const res = await fn(1, 2) @@ -110,7 +116,7 @@ describe('fromSuccess', () => { const a = composable(() => 1) const c = fromSuccess(a) - type _R = Expect Promise>> + type _R = Expect Promise<1>>> assertEquals(await c(), 1) }) From fb35be6bfc1b95c505084e2096b899ec6d7e1c39 Mon Sep 17 00:00:00 2001 From: Guga Guichard Date: Fri, 7 Jun 2024 13:42:42 -0300 Subject: [PATCH 2/3] Fix typo in test description --- src/tests/constructors.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/constructors.test.ts b/src/tests/constructors.test.ts index 6a596076..6d20dd49 100644 --- a/src/tests/constructors.test.ts +++ b/src/tests/constructors.test.ts @@ -151,7 +151,7 @@ describe('withSchema', () => { assertEquals(await handler(), success('no input!')) }) - it('ignores the input and pass undefined', async () => { + it('defaults non-declared input to unknown', async () => { const handler = withSchema()((args) => args) type _R = Expect< Equal< From 17ffdb6ed8e41ca88bf55e3d8b64feee42b7573c Mon Sep 17 00:00:00 2001 From: Guga Guichard Date: Fri, 7 Jun 2024 14:05:13 -0300 Subject: [PATCH 3/3] Lil refactor --- src/constructors.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/constructors.ts b/src/constructors.ts index 058d4fb5..229e2027 100644 --- a/src/constructors.ts +++ b/src/constructors.ts @@ -33,7 +33,7 @@ function toError(maybeError: unknown): Error { */ function composable( fn: T, -): Composable any>> { +): Composable any ? T : never> { return async (...args) => { try { // deno-lint-ignore no-explicit-any