From d8637d69836f1483c705821a63ac3dd4530c6538 Mon Sep 17 00:00:00 2001 From: Gil Pedersen Date: Sat, 26 Oct 2024 14:25:40 +0200 Subject: [PATCH] Improve boomify typings --- lib/index.d.ts | 7 +++---- test/index.ts | 5 +++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index 1947522..3ed9367 100755 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -156,10 +156,9 @@ export function isBoom(obj: unknown, statusCode?: number): obj is Boom; * * @returns A boom object */ -export function boomify(err: Boom, options?: BoomifyOptions & { decorate: Decoration }): Boom & Decoration; -export function boomify(err: unknown, options?: BoomifyOptions & { decorate: Decoration }): Boom & Decoration; -export function boomify(err: Boom, options?: BoomifyOptions): Boom; -export function boomify(err: unknown, options?: BoomifyOptions): Boom; +export function boomify(err: Terr, options: BoomifyOptions & { decorate: Decoration, data: Data }): (Terr extends Boom ? Terr : Boom) & Omit; +export function boomify(err: Terr, options: BoomifyOptions & { decorate: Decoration }): (Terr extends Boom ? Terr : Boom) & Decoration; +export function boomify(err: Terr, options?: BoomifyOptions): Terr extends Boom ? Terr : Boom; // 4xx Errors diff --git a/test/index.ts b/test/index.ts index 387e0d9..b4cbd48 100755 --- a/test/index.ts +++ b/test/index.ts @@ -50,12 +50,17 @@ expect.type(boom.output.headers); // boomify() const error = new Error('Unexpected input'); +class BadaBoom extends Boom.Boom {} expect.type(Boom.boomify(error, { statusCode: 400 })); expect.type(Boom.boomify(error, { statusCode: 400, message: 'Unexpected Input', override: false })); expect.type(Boom.boomify(error, { decorate }).x); expect.type(Boom.boomify('error')); expect.type>(Boom.boomify(new Boom.Boom<{ foo: 'bar' }>())); +expect.type(Boom.boomify(error, { decorate: { data: 1 } }).data); +expect.type(Boom.boomify(error, { decorate: { data: 1 }, data: 'bla' }).data); +expect.type(Boom.boomify(error, { data: 'bla' }).data); +expect.type(Boom.boomify(new BadaBoom(), { statusCode: 400 })); expect.error(Boom.boomify(error, { statusCode: '400' })); expect.error(Boom.boomify(error, { statusCode: 400, message: true }));