From 4978291ef8277b113b0473e8b407d3a8c80399be Mon Sep 17 00:00:00 2001 From: w01fgang Date: Sun, 29 Jun 2025 18:31:47 +0800 Subject: [PATCH 1/2] :sparkles: enhance Try class with TypeScript type safety in default method and add new test case Signed-off-by: w01fgang --- src/__tests__/Try.test.ts | 12 ++++++++++++ src/nextjs/index.ts | 10 ++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/__tests__/Try.test.ts b/src/__tests__/Try.test.ts index 822d9c8..775e009 100644 --- a/src/__tests__/Try.test.ts +++ b/src/__tests__/Try.test.ts @@ -216,4 +216,16 @@ describe('Try', () => { await expect(exec).rejects.toThrow('validation error'); }); + + it('should not give typescript error', async () => { + const params = { parameterKey: 'alpha' }; + + const result = await new Try(throwingFunction, params) + .default({ ok: true }) + .value(); + + expect(result).not.toBe(undefined); + expect(() => result.ok).not.toThrow(TypeError); + expect(Sentry.captureException).not.toHaveBeenCalled(); + }); }); diff --git a/src/nextjs/index.ts b/src/nextjs/index.ts index 22fc62b..92ed587 100644 --- a/src/nextjs/index.ts +++ b/src/nextjs/index.ts @@ -202,8 +202,14 @@ export class Try[] = Record(defaultValue: Return): Try { - return this.setConfig({ defaultValue }); + default(defaultValue: D): Omit & { value(): Promise | D> } { + type WithGuaranteedValue = Omit & { + value(): Promise | D>; + }; + + // Cast is safe: runtime shape is unchanged; this only narrows the static + // return type information for the `value` method. + return this.setConfig({ defaultValue }) as unknown as WithGuaranteedValue; } /** From 928412ce2f587f7ff0f7a1d6f3587a66d51a3d09 Mon Sep 17 00:00:00 2001 From: w01fgang Date: Sun, 29 Jun 2025 19:38:14 +0800 Subject: [PATCH 2/2] :bookmark: bump version to 0.0.6 and add typecheck script Signed-off-by: w01fgang --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 50400fb..fb63e1d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@power-rent/try-catch", - "version": "0.0.5", + "version": "0.0.6", "description": "A TypeScript utility for simplified async error handling with Sentry integration", "main": "dist/index.js", "module": "dist/esm/index.js", @@ -29,6 +29,7 @@ "test": "vitest run", "test:watch": "vitest", "clean": "rm -rf dist", + "typecheck": "tsc --noEmit", "prepublishOnly": "npm run clean && npm run build && npm run test" }, "keywords": [