Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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": [
Expand Down
12 changes: 12 additions & 0 deletions src/__tests__/Try.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
10 changes: 8 additions & 2 deletions src/nextjs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,14 @@ export class Try<T, TArgs extends readonly Record<string, any>[] = Record<string
* .value(); // Returns null if findUser throws
* ```
*/
default<Return>(defaultValue: Return): Try<T, TArgs> {
return this.setConfig({ defaultValue });
default<D>(defaultValue: D): Omit<typeof this, 'value'> & { value(): Promise<Awaited<T> | D> } {
type WithGuaranteedValue = Omit<typeof this, 'value'> & {
value(): Promise<Awaited<T> | 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;
}

/**
Expand Down