days
-3
-6
hours
-2
-2
minutes
-4
0
seconds
-1
-3
search
A feature five years in the making

TypeScript 3.7 adds long-awaited optional chaining feature

Sarah Schlothauer
typescript
©Shutterstock / BrAt82

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.

TypeScript, the superset of JavaScript, offers minimal configuration management and IDE support, while still providing the benefit of JavaScript libraries. It adds optional static type-checking and type syntax and works with all browsers and operating systems.

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.

Optional chaining

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 null or undefined.  An optional chain is an optional expression which is then followed by either regular property access, element access, or call argument lists.

SEE ALSO: Containers and security – What are the five biggest myths?

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

View the pull request and proposal for more information.

Nullish Coalescing

The nullish coalescing operator works in tandem with optional chaining as a complementary operator. Nullish coalescing uses the ?? operator.

Now, 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 null or undefined. When we write code like

let x = foo ?? bar();

this is a new way to say that the value foo will be used when it’s “present”; but when it’s null or undefined, calculate bar() in its place.

Again, the above code is equivalent to the following.

let x = (foo !== null && foo !== undefined) ?

    foo :

    bar();

Check out the pull request and proposal for a closer look.

3.7 updates

Nullish coalescing and optional chaining aren’t the only features arriving in this update.

Some of the other features include:

SEE ALSO: Top programming languages for November 2019: C gets an A+

  • Assertion signatures: New assertion signatures model upon JavaScript assertion functions.
  • New recursive type aliases: Restrictions on type aliases have been relaxed.
  • useDefineForClassFields flag: Declarations initialized with  Object.defineProperty; declarations now always initialized to undefined
  • 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.

Author
Sarah Schlothauer

Sarah Schlothauer

All Posts by Sarah Schlothauer

Sarah Schlothauer is an assistant editor for JAXenter.com. She received her Bachelor's degree from Monmouth University and is currently enrolled at Goethe University in Frankfurt, Germany where she is working on her Masters. She lives in Frankfurt with her husband and cat.

Leave a Reply

Be the First to Comment!

avatar
400
  Subscribe  
Notify of