db-errors
db-errors copied to clipboard
TypeScript types error as unknown
When trying to use this library
try {
return await this.exampleRepository.save(input);
} catch (error) {
const wrapped = wrapError(error);
if (wrapped instanceof UniqueViolationError) {
throw new ExampleAlreadyExists(wrapped);
}
throw error;
}
TypeScript complains because error is of type unknown rather than Error
const wrapped = wrapError(error);
// ^^^^^
// Argument of type 'unknown' is not assignable to parameter of type 'Error'.
Because of changes in TypeScript 4.4 https://devblogs.microsoft.com/typescript/announcing-typescript-4-4/#use-unknown-catch-variables
User needs to cast the error to Error, however, it does not seem that the code would fail if the error were any other type, so perhaps the typescript d.ts can be changed?