From 6faad9d19989039cc3ab4e053d97028780cc7e9b Mon Sep 17 00:00:00 2001 From: Niklas Wulf Date: Wed, 17 Jan 2018 18:57:36 +0100 Subject: [PATCH] [boom] isBoom is a type-guard (#22918) Instead of just returning boolean, the `isBoom` function can and should be a type-guard, which enables the obvious benefits. --- types/boom/boom-tests.ts | 6 ++++++ types/boom/index.d.ts | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/types/boom/boom-tests.ts b/types/boom/boom-tests.ts index f989128b4e..4d133b2545 100644 --- a/types/boom/boom-tests.ts +++ b/types/boom/boom-tests.ts @@ -144,6 +144,12 @@ const isBoomError = new Boom.Boom('test') Boom.isBoom(isBoomError); +const maybeBoom = new Boom.Boom('test'); +if(Boom.isBoom(maybeBoom)) { + // isBoom is a type guard that allows accessing these properties: + maybeBoom.output.headers; +} + // constructor const constructorError: Boom.Boom = new Boom.Boom('test'); diff --git a/types/boom/index.d.ts b/types/boom/index.d.ts index 89ca580561..ed48f181cf 100644 --- a/types/boom/index.d.ts +++ b/types/boom/index.d.ts @@ -86,7 +86,7 @@ declare namespace Boom { * Identifies whether an error is a Boom object. Same as calling instanceof Boom. * @param error the error object to identify. */ - export function isBoom(error: Error): boolean + export function isBoom(error: Error): error is Boom // 4xx /**