From c7953e288ae14ff157370c9a89a08703e72ddd12 Mon Sep 17 00:00:00 2001 From: Benny Yen Date: Wed, 2 Apr 2025 09:52:21 +0000 Subject: [PATCH] fix: update `then` method signature --- src/p.test.ts | 9 ++++++++- src/p.ts | 11 +++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/p.test.ts b/src/p.test.ts index 33c39f6..26a943a 100644 --- a/src/p.test.ts +++ b/src/p.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest' +import { describe, expect, expectTypeOf, it } from 'vitest' import { p as P } from './p' const timeout = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)) @@ -71,4 +71,11 @@ describe('should', () => { expect(e).toBeInstanceOf(TypeError) } }) + + it('should return the correct type', async () => { + const p = P([1, 2, 3]) + expectTypeOf(await p).toEqualTypeOf() + // @see issue #47 + expectTypeOf(await p.map(async i => i).map(async i => i)).toEqualTypeOf() + }) }) diff --git a/src/p.ts b/src/p.ts index 752aa40..3772731 100644 --- a/src/p.ts +++ b/src/p.ts @@ -79,12 +79,11 @@ class PInstance extends Promise[]> { this.promises.clear() } - then(fn?: () => PromiseLike) { - const p = this.promise - if (fn) - return p.then(fn) - else - return p + then[], TResult2 = never>( + onfulfilled?: ((value: Awaited[]) => TResult1 | PromiseLike) | null, + onrejected?: ((reason: any) => TResult2 | PromiseLike) | null, + ) { + return this.promise.then(onfulfilled, onrejected) } catch(fn?: (err: unknown) => PromiseLike) {