TypeScript 4.4 breaking changes
TypeScript has several breaking changes in the 4.4 release. One of these changes, namely, using unknown
type for catch variables in the --strict
mode might result in strange error messages such as:
src/ApplicationError.spec.ts:22:16 - error TS2571: Object is of type 'unknown'.
22 expect(error.message).toStrictEqual(errorDetail);
~~~~~
src/ApplicationError.spec.ts:23:16 - error TS2571: Object is of type 'unknown'.
23 expect(error.name).toStrictEqual(ApplicationError.name);
~~~~~
You can find the explanation in the list of breaking changes for the release. This is a minor release. And this is why compilation errors like this might come as a surprise.
I would recommend adding a check for error variable type to fix the issue. For example:
...
} catch (error) {
// Check common Error properties:
if (!(error instanceof Error)) {
fail("Error should be thrown.");
}
expect(error.message).toStrictEqual(errorDetail);
expect(error.name).toStrictEqual(ApplicationError.name);
...
Another option would be to add an explicit any
annotation. For example:
...
} catch (error: any) {
expect(error.message).toStrictEqual(errorDetail);
expect(error.name).toStrictEqual(ApplicationError.name);
...