TypeScript 4.4, released on August 26, 2021, changes what a project’s existing strict setting asks of its error handlers. Catch variables now default to unknown under the new useUnknownInCatchVariables option, which strict enables. Code that reads an unchecked caught value’s message, name or stack can therefore produce new compiler errors. The migration issue is an assumption inside the handler: JavaScript can throw values other than Error objects. [S1]
In August 2020, TypeScript 4.0 allowed developers to annotate an individual catch variable as unknown, but left the default unchanged. TypeScript 4.4 moves that stricter treatment into the strict option family. That means an upgrade can expose a handler the old configuration accepted, without anyone deliberately tightening the settings. [S1][S2]
The useful repair depends on what the handler needs to do. Microsoft’s 4.4 example checks whether the caught value is an instance of Error before reading its message. The 4.0 example tests whether the value is a string before calling a string method. Those checks establish different facts; neither is a reason to assume every exception has the same shape. For application owners, the practical task is to decide how each handler should deal with values outside the type it expects. [S1][S2]
There are escape hatches. Microsoft documents an explicit any annotation on the catch variable, a type assertion, or disabling useUnknownInCatchVariables. These choices are not equivalent to checking the value at runtime: TypeScript erases types from the generated JavaScript. A local any annotation can exempt one handler while leaving the default check elsewhere; switching off the option removes that default more broadly. Keeping exceptions local is a migration choice for the team, not a requirement of the release. [S1]
Start the upgrade review with catch blocks that directly read message, name or stack—the properties Microsoft names in its compatibility warning. Separate handlers that need a runtime check from deliberate exceptions, then test the expected Error path alongside non-Error values. A non-Error value still needs an application response, even after the compiler errors are gone. [S1]
Source documents
- Announcing TypeScript 4.4https://devblogs.microsoft.com/typescript/announcing-typescript-4-4/
- Announcing TypeScript 4.0https://devblogs.microsoft.com/typescript/announcing-typescript-4-0/
