TypeScript 5.6 Tightens Truthy And Nullish Checks
Written by Ian Elliot   
Monday, 16 September 2024

TypeScript 5.6 has been released. The update has better handling of truthy and nullish checks and new iterator helper methods.

TypeScript, the a superset of JavaScript that adds optional static types which can be checked by the TypeScript compiler to catch common errors in your programs, is now much more widely used than JavaScript itself, according to both the Stack Overflow Developer Survey and the State of JavaScript Survey, see The Ongoing State Of JavaScript. Part of the reason for TypeScript's popularity is perhaps its ability to help you avoid about mistakes like typos, missing arguments, or forgetting to check for null and undefined and the latest release go9es further in eliminating bugs.


The first improvement to TypeScript 5.6 is in the way it handles checks for truthy and nullish. In the past, expressions that have "static truthy semantics" were allowed. Such expressions always evaluate to the same truthiness, and the TypeScript team says that when experimenting, they found that many bugs could be caught by flagging up code that always evaluates in a specific way, to be either always truthy or always nullish. In TypeScript 5.6, the compiler now returns an error when it can syntactically determine a truthy or nullish check will always evaluate in a specific way.  However, some expressions still are allowed even if truthy or nullish. The list of allowable expressions include true, false, 0, and 1.

Iterator helper methods have also been added to this release. The improvement adds methods previously limited to arrays onto most of the iterators that are produced in JavaScript. Iterables are things that you can iterate over over by calling a [Symbol.iterator]() to get an iterator, while iterators are things that have a next() method that can be used to get the next value as you iterate. The TypeScript team says developers use iterables in all sorts of places in JavaScript, but until now had missing methods on Arrays like map, filter, and reduce. This release adds those and other methods onto most of the IterableIterators that are produced in JavaScript.

Another addition, strict built-in iterator checks, arose from the addition of interator helper methods. The developers say that with the new IteratorObject type, they discovered some difficulties in allowing safe implementations of IteratorObjects. This added to a long standing safety issue with IteratorResult in cases where TReturn was any, which is the default.

While it would be hard to fix the problem on every Iterator without introducing a lot of breaks, the team says they can at least fix it with most IteratorObjects that get created. TypeScript 5.6 introduces a new intrinsic type called BuiltinIteratorReturn and a new --strict-mode flag called --strictBuiltinIteratorReturn. Whenever IteratorObjects are used in places like lib.d.ts, they are always written with BuiltinIteratorReturn type for TReturn. This will produce an accurate and type-safe return type for the next() methods of iterators produced by built-ins like Array, Set, and Map.

TypeScript 5.6 is available now.

More Information

TypeScript On NuGet

TypeScript On GitHub

Related Articles

TypeScript 5.5 Adds ECMAScript Set Support

Node.js Adds Experimental TypeScript Support

TypeScript 5 - Smaller, Simpler, Faster

The Ongoing State Of JavaScript

 

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


The Generation That Can Text But Not Type
08/09/2024

Gen Z-ers, those born between 1997 and 2012 and who have grown up with smartphones, are probably the most tech-savvy generation. But when it comes to working with computers, there is an important skil [ ... ]



A Cheaper Raspberry Pi 5
20/08/2024

There's a new Raspberry Pi 5, costing just $50. This price reduction has been achieved by cutting memory to 2MB, plenty for most applications, and eliminating the "dark silicon" that takes up space an [ ... ]


More News

kotlin book

 

Comments




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

 

Last Updated ( Monday, 16 September 2024 )