From cae742417beaaa03f429ab7d62b496630f0d9757 Mon Sep 17 00:00:00 2001 From: Sergei Zharinov Date: Sun, 24 Nov 2024 14:45:56 -0300 Subject: [PATCH] refactor: Error type narrowing for `Result` class (#32705) --- lib/util/result.spec.ts | 19 ++++++++------- lib/util/result.ts | 51 ++++++++++++++++++----------------------- 2 files changed, 31 insertions(+), 39 deletions(-) diff --git a/lib/util/result.spec.ts b/lib/util/result.spec.ts index 775c077ebd43b4..61b0957ca8e29c 100644 --- a/lib/util/result.spec.ts +++ b/lib/util/result.spec.ts @@ -240,9 +240,8 @@ describe('util/result', () => { }); it('converts error to Result', () => { - const result = Result.err('oops').catch(() => - Result.ok(42), - ); + const error: Result = Result.err('oops'); + const result = error.catch((_err) => Result.ok(42)); expect(result).toEqual(Result.ok(42)); }); @@ -600,15 +599,15 @@ describe('util/result', () => { describe('Catch', () => { it('converts error to AsyncResult', async () => { - const result = await Result.err('oops').catch(() => - AsyncResult.ok(42), - ); + const error: Result = Result.err('oops'); + const result = await error.catch(() => AsyncResult.ok(42)); expect(result).toEqual(Result.ok(42)); }); it('converts error to Promise', async () => { const fallback = Promise.resolve(Result.ok(42)); - const result = await Result.err('oops').catch(() => fallback); + const error: Result = Result.err('oops'); + const result = await error.catch(() => fallback); expect(result).toEqual(Result.ok(42)); }); @@ -619,9 +618,9 @@ describe('util/result', () => { }); it('converts AsyncResult error to Result', async () => { - const result = await AsyncResult.err('oops').catch(() => - AsyncResult.ok(42), - ); + const error: AsyncResult = + AsyncResult.err('oops'); + const result = await error.catch(() => AsyncResult.ok(42)); expect(result).toEqual(Result.ok(42)); }); }); diff --git a/lib/util/result.ts b/lib/util/result.ts index 7237d0d33d67b4..f6649b65028faa 100644 --- a/lib/util/result.ts +++ b/lib/util/result.ts @@ -480,28 +480,23 @@ export class Result { } catch( - fn: (err: E) => Result, - ): Result; + fn: (err: E) => Result, + ): Result; catch( - fn: (err: E) => AsyncResult, - ): AsyncResult; + fn: (err: E) => AsyncResult, + ): AsyncResult; catch( - fn: (err: E) => Promise>, - ): AsyncResult; + fn: (err: E) => Promise>, + ): AsyncResult; catch( - fn: ( - err: E, - ) => - | Result - | AsyncResult - | Promise>, - ): Result | AsyncResult { + fn: (err: E) => Result | AsyncResult | Promise>, + ): Result | AsyncResult { if (this.res.ok) { - return this; + return this as never; } if (this.res._uncaught) { - return this; + return this as never; } try { @@ -833,25 +828,23 @@ export class AsyncResult } catch( - fn: (err: NonNullable) => Result, - ): AsyncResult; + fn: (err: NonNullable) => Result, + ): AsyncResult; catch( - fn: (err: NonNullable) => AsyncResult, - ): AsyncResult; + fn: (err: NonNullable) => AsyncResult, + ): AsyncResult; catch( - fn: (err: NonNullable) => Promise>, - ): AsyncResult; + fn: (err: NonNullable) => Promise>, + ): AsyncResult; catch( fn: ( err: NonNullable, - ) => - | Result - | AsyncResult - | Promise>, - ): AsyncResult { - const caughtAsyncResult = this.asyncResult.then((result) => - // eslint-disable-next-line promise/no-nesting - result.catch(fn as never), + ) => Result | AsyncResult | Promise>, + ): AsyncResult { + const caughtAsyncResult: Promise> = this.asyncResult.then( + (result) => + // eslint-disable-next-line promise/no-nesting + result.catch(fn as never), ); return AsyncResult.wrap(caughtAsyncResult); }