Happy Hacking

moon indicating dark mode
sun indicating light mode

TypeScript 3.7 (Beta)

October 02, 2019

In the last post I've wrote about how you can incrementally bring the benefits of TypeScript's strict mode to all engineers on your team. Almost like a incredible coincidence TypeScript 3.7 Beta has been released today, including some of our most highly-requested features like Optional Chaining and Nullish Coalescing. Those features can help you write cleaner code when it comes down to Null Checking.

Let's have a look at an abstract of our coding conventions:

Reason

  • && the value on the right of this operator will only be used when the expression on the left side of the operator evaluates to null or undefined
  • || the value on the right of this operator will be used when the expression on the left of it evaluates to any falsy value (e.g. "", NaN, 0 or false)

Don't

const volume = (storage && storage.settings && storage.settings.volume) || 0.5;

Do

const volume = storage?.settings?.volume ?? 0.5

In the example using && and || the volume would be evaluated to 0.5 even tough the value in the store was set to 0.

That's it. Happy Hacking. ✌️