Skip to content

Commit 27c8b50

Browse files
committed
Narrow error type for ignore()
1 parent 47d9144 commit 27c8b50

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

lib/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ export function ignore<E extends Error, D extends Decoration>(err: any, types: T
6363
export function ignore<T extends object, D extends Decoration>(err: T, types: TypeRule | TypeRule[], options: BounceOptions & { return: true, decorate: D }): (T & D) | undefined;
6464
export function ignore<E extends Error>(err: any, types: TypeRule | TypeRule[], options: BounceOptions & { return: true, override: E }): E | undefined;
6565
export function ignore<T>(err: T, types: TypeRule | TypeRule[], options: BounceOptions & { return: true }): T | undefined;
66+
export function ignore(err: any, types: 'boom', options: { return?: false | undefined }): asserts err is Boom;
67+
export function ignore(err: any, types: 'boom'): asserts err is Boom;
68+
export function ignore(err: any, types: TypeRule | TypeRule[], options: { return?: false | undefined }): asserts err is Error;
69+
export function ignore(err: any, types: TypeRule | TypeRule[]): asserts err is Error;
6670
export function ignore(err: any, types: TypeRule | TypeRule[], options?: BounceOptions): void;
6771

6872
type Action = 'rethrow' | 'ignore';

test/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,20 @@ expect.type<TypeError | undefined>(Bounce.ignore(new TypeError(), 'boom', { retu
2929
expect.type<RangeError | undefined>(Bounce.ignore(null, 'boom', { return: true, override: new RangeError() }));
3030
expect.type<(TypeError & { prop: string }) | undefined>(Bounce.ignore(new TypeError(), 'boom', { return: true, decorate: { prop: 'ok' } }));
3131

32+
// Narrows the error type
33+
34+
{
35+
const error = new TypeError() as any;
36+
Bounce.ignore(error, TypeError);
37+
expect.type<Error>(error);
38+
}
39+
40+
{
41+
const error = new Boom.Boom() as any;
42+
Bounce.ignore(error, 'boom');
43+
expect.type<Boom.Boom>(error);
44+
}
45+
3246
expect.error(Bounce.ignore(new Error()));
3347
expect.error(Bounce.ignore(new Error(), 'unknown'));
3448
expect.error(Bounce.ignore(new Error(), 'boom', true));

0 commit comments

Comments
 (0)