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


Personal Picks For Holiday Gifts
29/11/2024

It's Black Friday, the traditional day to indulge in online shopping. Not every item that is included in my selection of gifts is subject to a promotional offer, but where they aren't you might be poi [ ... ]



Pico 2W Announced But There Is A Surprise!
25/11/2024

Raspberry Pi released the Pico 2 a few months ago and we have been waiting for the Pico 2W since then. But Pimoroni beat them to the draw with the Pico Plus 2W based on the RM2 radio module and hinted [ ... ]


More News

espbook

 

Comments




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

 

Last Updated ( Monday, 16 September 2024 )