Typescript 3.7

The new version of TypeScript has been released and with it comes optional chaining as well as nullish coalescing!

5th Nov. 2019

Elvis (operator) is in the house 🎉!

I have been waiting for these two operators for a while now and it seems its not only just me. In the 5 years that TypeScript has gone open-source, people have been asking for them since these operators do not exist in JavaScript.

Optional chaining

Optional chaining allows developers to write code that stops early where expressions are null or undefined. If you are familiar with the Elvis operators in C# this would look familiar to you.

// person object exists somewhere
// and could be null or undefined

const fullName = person?.fullName

person?.activate()

As you can see, it is more concise. Previously it could look something like this:

const fullName = !!person ? person.fullName : undefined

if (!!person) {
  person.activate()
}

Nullish coalescing

This operators allow us to define a default value if we encounter null or undefined. Take the example below.

const fullName = person.fullName ?? ‘John Smith’

Previously, I would do something like this:

const fullName = person.fullName || ‘John Smith’

More information about TypeScript 3.7 and the new features it brings can be seen here.