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. TypeScript is a superset of JavaScript that adds optional static types which can be checked by the TypeScript compiler to catch common errors in your programs.
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. The next improvement is to catch variables, used in JavaScript to handle the catch clause when a value has been thrown with throw. Because JavaScript let developers throw any type of value, TypeScript always typed catch clause variables as any, and wouldn't allow any other type annotation. 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: --exactOptionalPropertyTypes 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.
More InformationRelated ArticlesTypeScript 4.3 Adds Separate Property Write Types TypeScript 4.2 Release Candidate Available New TypeScript Website Launched TypeScript 4.1 Adds Temporal Literal Types TypeScript 3.0 Adds Project References TypeScript Adds Unused Span Reporting TypeScript Adds Conditional Types
To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info |
|||
Last Updated ( Thursday, 08 July 2021 ) |