Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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