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.

typeslogo

 

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.

typeslogo

 

  • Ian Elliot is the author of several JavaScript titles. Just JavaScript: An Idiomatic Approach is intended for programmers who are familiar with another language, and which takes a radical look at JavaScript that takes account of the way it is object-based. JavaScript Async covers asynchronous programming in JavaScript, async/await, Promises, Service Workers and so on.  His latest book, JavaScript Bitmap Graphics with Canvas shows you how to use Canvas to create graphics without resorting to a library of any kind.

More Information

TypeScript On NuGet

TypeScript On GitHub

Related Articles

TypeScript 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.

Banner


Is PHP in Trouble?
10/04/2024

The April 2024 headline for the TIOBE Index, which ranks programming languages in terms of their popularity, reads, "Is PHP losing its mojo" asking this question because this month PHP has dropped out [ ... ]



Apache Shiro 2.0 Released
21/03/2024

Apache Shiro 2.0 has been released. The Java security framework now requires at least Java 11, and has added support for Jakarta EE 10.


More News

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Thursday, 08 July 2021 )