Support plan
- is this issue currently blocking your project? (yes/no): yes
- is this issue affecting a production system? (yes/no): yes
Context
- node version: *
- module version: 9.1.4
- environment (e.g. node, browser, native): *
- used with (e.g. hapi application, another framework, standalone, ...): Typescript >= 4.4
- any other relevant information: *
What problem are you trying to solve?
Since typescript 4.4, the type of the err in a catch block will be "unknown".
try {
// stuff
} catch (err) {
throw boomify(err) // <=== typescript error "Argument of type "unknown" cannot be assigned to type: Error"
}
There are two solutions, both of which result in a lot of added code
try {
// stuff
} catch (err) {
throw err instanceof Error ? boomify(err) : err
}
// we have to import this everywhere when it is needed
const asError = (err: unknown): Error => err instanceof Error ? err : Object.assign(new Error(err?.message ?? 'Unknown error'), err)
try {
// stuff
} catch (err) {
throw boomify(asError(err))
}
Since boomify is already an error wrapper utility, having to do either of these is anoying.
Do you have a new or modified API suggestion to solve the problem?
Could we allow the first argument of boomify() to be of type unknown ?
If you don't want to change the current signature, we could also make this optional:
boomify(err as unknown, { allowUnknown: true })
Support plan
Context
What problem are you trying to solve?
Since typescript 4.4, the type of the
errin a catch block will be "unknown".There are two solutions, both of which result in a lot of added code
Since
boomifyis already an error wrapper utility, having to do either of these is anoying.Do you have a new or modified API suggestion to solve the problem?
Could we allow the first argument of
boomify()to be of typeunknown?If you don't want to change the current signature, we could also make this optional: