From eada6072d73e296d3c30bff30c4931279c8933b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boye=20Borg=20Nyg=C3=A5rd?= <6630430+boyeborg@users.noreply.github.com> Date: Thu, 12 Sep 2024 20:10:36 +0200 Subject: [PATCH 1/2] make fromThrowable type more strict --- src/result-async.ts | 13 +++++++++++-- src/result.ts | 21 ++++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/result-async.ts b/src/result-async.ts index 01f2f13e..57f33bb7 100644 --- a/src/result-async.ts +++ b/src/result-async.ts @@ -42,17 +42,26 @@ export class ResultAsync implements PromiseLike> { return new ResultAsync(newPromise) } + // eslint-disable-next-line @typescript-eslint/no-explicit-any + static fromThrowable( + fn: (...args: A) => Promise, + ): (...args: A) => ResultAsync + // eslint-disable-next-line @typescript-eslint/no-explicit-any + static fromThrowable( + fn: (...args: A) => Promise, + errorFn: (err: unknown) => E, + ): (...args: A) => ResultAsync // eslint-disable-next-line @typescript-eslint/no-explicit-any static fromThrowable( fn: (...args: A) => Promise, errorFn?: (err: unknown) => E, - ): (...args: A) => ResultAsync { + ): (...args: A) => ResultAsync { return (...args) => { return new ResultAsync( (async () => { try { return new Ok(await fn(...args)) - } catch (error) { + } catch (error: unknown) { return new Err(errorFn ? errorFn(error) : error) } })(), diff --git a/src/result.ts b/src/result.ts index f2b84989..ed59b283 100644 --- a/src/result.ts +++ b/src/result.ts @@ -17,18 +17,33 @@ export namespace Result { * arguments but returning `Ok` if successful, `Err` if the function throws * * @param fn function to wrap with ok on success or err on failure - * @param errorFn when an error is thrown, this will wrap the error result if provided */ // eslint-disable-next-line @typescript-eslint/no-explicit-any + export function fromThrowable any>( + fn: Fn, + ): (...args: Parameters) => Result, unknown> + /** + * Wraps a function with a try catch, creating a new function with the same + * arguments but returning `Ok` if successful, `Err` if the function throws + * + * @param fn function to wrap with ok on success or err on failure + * @param errorFn when an error is thrown, this will wrap the error result + */ + // eslint-disable-next-line @typescript-eslint/no-explicit-any + export function fromThrowable any, E>( + fn: Fn, + errorFn: (e: unknown) => E, + ): (...args: Parameters) => Result, E> + // eslint-disable-next-line @typescript-eslint/no-explicit-any export function fromThrowable any, E>( fn: Fn, errorFn?: (e: unknown) => E, - ): (...args: Parameters) => Result, E> { + ): (...args: Parameters) => Result, E | unknown> { return (...args) => { try { const result = fn(...args) return ok(result) - } catch (e) { + } catch (e: unknown) { return err(errorFn ? errorFn(e) : e) } } From 67360794043d21244fafe1dea0c088d5fdd0cd2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boye=20Borg=20Nyg=C3=A5rd?= <6630430+boyeborg@users.noreply.github.com> Date: Thu, 12 Sep 2024 20:27:08 +0200 Subject: [PATCH 2/2] add changeset --- .changeset/grumpy-hats-unite.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/grumpy-hats-unite.md diff --git a/.changeset/grumpy-hats-unite.md b/.changeset/grumpy-hats-unite.md new file mode 100644 index 00000000..dc0736d8 --- /dev/null +++ b/.changeset/grumpy-hats-unite.md @@ -0,0 +1,5 @@ +--- +'neverthrow': minor +--- + +prevent passing a specific error type for `.fromThrowable` without providing an error mapping function