Optional chaining makes it easier to write code that interrogates nested data and which is able to handle unexpected changes in the data structure.

A near-future update to the JavaScript spin-off TypeScript will add support for the long-awaited optional chaining feature.

Optional chaining makes it easier to write code that interrogates nested data and which is able to handle unexpected changes in the data structure.

Must-read Developer content

For those unaware, TypeScript is a superset of JavaScript developed by Microsoft, which adds optional type checking and other features to make JavaScript better suited to writing large and complex code bases.

In recent years, the popularity of TypeScript has risen sharply, and today is used by major companies such as Slack and Microsoft.

To understand the benefits of optional chaining — due to be implemented in the 3.7.0 release — consider a nested data structure such as the following object:

const pupil = { student: {name: "Joe Bloggs", grades: {module1: "A",module2:"B"}, subject: "Physics"}}

To programmatically extract the student's grade, you might write the following JavaScript

const gradeMod1 = pupil.student.grades.module1

However, if the structure of the object were to change with a subsequent student to say:  

const pupil = { student: {name: "Joan Bloggs", subject: "Physics"}}

Following this change in structure, the code const grade = pupil.student.grades.module1 would throw a TypeError for trying to read the property module1 of null, as the intermediate property grades no longer exists.

Optional chaining provides new syntax to allow developers to easily check these nested data structures still exist when extracting data in this way, allowing them to sidestep issues that would otherwise be thrown up by trying to check properties of null values.

SEE: How to build a successful developer career (free PDF) (TechRepublic)

In the example above, using optional chaining would see the developer prepending the ? operator to each property when accessing the data, for example.

const grade = pupil?.student?.grades?.module1

This would have the effect of checking whether each intermediate property — pupil, student, grades — existed when extracting the value of module1 and return undefined if any intermediate property were missing.

As well as being used when retrieving a value from an object's property, optional chaining can also be used when accessing an optional dynamic property of an object, for example, obj?.[expr], or when making an optional function or method call, for example, func?.(...args).

TypeScript 3.7 will also add another feature that complements optional chaining, known as nullish coalescing, which provides a new ?? operator that can handle false values in a more predictable manner.

New versions of TypeScript are typically released every two months or so, with version 3.6 due out shortly, bringing with it strongly typed iterators and generators, more accurate array spreads, and an improved user experience when handling Promises.

Optional chaining is now also a stage 3 ECMAscript feature, which means it's getting closer to eventually being a standard feature for JavaScript as a whole.

TypeScript is even inspiring other languages, with Python creator Guido van Rossum recently telling an audience of developers that Python was learning lessons from TypeScript's optional type checking.

If you're interested in finding out more about TypeScript, check out TechRepublic's round-up of the best free resources for learning the language online.

Also see 

Programmer working with program code

Image: iStockphoto/RossHelen