|TypeScript 4.4 Improves Control Flow Analysis|
|Written by Ian Elliot|
|Thursday, 08 July 2021|
TypeScript's latest beta, 4.4, has been released with support for control flow analysis of aliased conditions, along with symbol and template string pattern index signatures.
The ability to use control flow analysis for aliased conditions means you'll no longer get an error if you use a type guard where the condition is in a constant. TypeScript already had the ability to check whether a variable had a more specific type, and the type-checker uses control flow analysis to deduce the type within every language construct. However, until now, if you chose to store the condition in a constant, as in
const argIsString = typeof arg === "string";
then TypeScript couldn't use that condition in a type guard check. It now supports this.
The next improvement is support for symbol and template string pattern index signatures. TypeScript uses index signatures so developers can describe objects where every property has to have a certain type. This means developers can use such objects as dictionary-like types, with string keys to index into them with square brackets.
Until now index signatures have been limited to string and number keys, so you couldn't index objects with symbol keys or with subsets of string keys. TypeScript 4.4 addresses these limitations, and allows index signatures for symbols and template string patterns.
Once TypeScript added the unknown type, that was a better choice than any in catch clause variables for users who want the highest degree of correctness and type-safety, and from TypeScript 4.0 you could specify an explicit type annotation of unknown (or any) on each catch clause variable. This was laborious, so TypeScript 4.4 introduces a new flag ???called --useUnknownInCatchVariables. This flag changes the default type of catch clause variables from any to unknown.
Other improvements include a new flag:
for optional property types that specifies that they should be interpreted exactly as written, meaning that | undefined is not added to the type.
TypeScript 4.4 is available now as a beta release.
or email your comment to: email@example.com
|Last Updated ( Thursday, 08 July 2021 )|