TypeScript 3.7 adds long-awaited optional chaining feature
The newest version of TypeScript arrived on November 5, 2019. It includes new tooling features, optional chaining, nullish coalescing, assertion functions, and much more. TypeScript is on the up and up, according to the spring Technology Radar report, when it moseyed on up to the ‘adopt’ sector. See what’s new in the open source programming language.
The latest release has now been announced. Introducing TypeScript 3.7. Two of the biggest additions with this update include optional chaining and a nullish coalescing operator.
Newcomers can test out the language in the playground and see how it all works. This time around, the playground also received a few updates, including a quick fix feature, dark and high-contrast mode, and the ability to import other packages.
The long wait finally ends. The issue regarding optional chaining was added five years ago, in 2014, back when TypeScript was still new. Now, one of the most requested features is officially implemented and standardized. Optional chaining reached its Stage 3 consensus.
Optional chaining allows you to write code which stops running some expressions if it hits a
undefined. An optional chain is an optional expression which is then followed by either regular property access, element access, or call argument lists.
The operator uses
From the proposal, it may appear in three positions:
obj?.prop // optional static property access obj?.[expr] // optional dynamic property access func?.(...args) // optional function or method call
The nullish coalescing operator works in tandem with optional chaining as a complementary operator. Nullish coalescing uses the
?? operator can replace instances of
|| when trying to use a default value.
From the TypeScript 3.7 announcement blog:
You can think of this feature – the
??operator – as a way to “fall back” to a default value when dealing with
undefined. When we write code like
let x = foo ?? bar();
this is a new way to say that the value
foowill be used when it’s “present”; but when it’s
bar()in its place.
Again, the above code is equivalent to the following.
let x = (foo !== null && foo !== undefined) ?
Nullish coalescing and optional chaining aren’t the only features arriving in this update.
Some of the other features include:
- New recursive type aliases: Restrictions on type aliases have been relaxed.
useDefineForClassFieldsflag: Declarations initialized with
Object.defineProperty; declarations now always initialized to
- Flags non-nullable function in ‘if’ statements as errors
- Semicolon formatting: Built-in formatter support semicolon insertion and removal in instances where trailing semicolons are optional. Works with Visual Studio Code Insiders and coming soon to Visual Studio 16.4 Preview 2.
- Improved error reporting: Error reports are now flattened and won’t extend outwards in long pyramids.
View the Microsoft dev blog for an exhaustive list of all the new features and breaking changes.
Get a head start on the next version. The TypeScript 3.8 plan is now available on GitHub. Notable expected items include top-level await.