This article is more than 1 year old
Optional (un)chained melodies for those who like their TypeScript to be more assertive in 3.7
Release Candidate drops just in time for Microsoft's Ignite
As its Florida Ignite shindig looms large, Microsoft has settled on a Release Candidate for TypeScript 3.7 ahead of an imminent launch.
The open-source TypeScript, a superset of JavaScript (replete with static typing if required), has been gaining popularity over the years. Along with the likes of Rust and Kotlin, it is threatening to crack the TIOBE Index top 20 and sits in the top 10 most popular languages, according to 2019's Stack Overflow Developer Survey.
A few short months since version 3.6 put in an appearance, 3.7 adds all manner of goodies to delight the faithful, including optional chaining (an implementation of pipeline chain operators made a long-overdue appearance in PowerShell 7 this week).
Optional chaining in the TypeScript world will stop an expression being run if a null
or undefined
is hit thanks to a new ?.
operator. An example of the usage would be:
let x = foo?.bar.baz();
The snippet would compute foo.bar.baz();
if foo
is defined, but return undefined
if foo
itself is null
or undefined
.
The operator also crops up in optional element access and optional calls, which allows expressions to be conditionally call if they are not null
or undefined
.
It is undoubtedly a handy function, and an ECMAScript feature, although we have to confess it made the flow of our code a little trickier to read until one was quite familiar with the operator.
Linked to the optional chaining is nullish coalescing, a way of falling back to a default value when a null
or undefined
is encountered. There are also new "assertion signatures" for typing purposes and improved support for functions that return never
.
Semicolon insertion and removal according to JavaScript's automatic semicolon insertion rules is supported (for VS Code Insiders and those content to run a preview of Visual Studio) and the whole editing experience has seen improvement. Where built dependencies might be out of date, TypeScript will now work with the source .ts
or .tsx
files instead in order to keep things up to date.
The feature can be turned off for particularly large projects.
The current plan calls for only minimal changes between now and the final TypeScript 3.7 release in "a couple of weeks."
Just in time for Ignite. ®