Does Typescript support the ?. operator? (And, what"s it called?)

Cover Image for Does Typescript support the ?. operator? (And, what"s it called?)
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

Does Typescript support the ?. operator? (And, what's it called?)

You know those pesky JavaScript errors that pop up when you try to access properties or methods on a null or undefined value? 😤 They can drive any developer insane! But fear not, because TypeScript has the perfect solution for this issue – the ?. operator, also known as the optional chaining operator. 🎉

What is the ?. operator?

The ?. operator allows you to safely access nested properties or methods without worrying about encountering a TypeError if any intermediate value along the chain happens to be null or undefined. It's like a protective shield for your code! 🛡️

Let's take a look at an example:

interface Foo {
  bar: {
    baz: number;
  } | null;
}

const foo: Foo = {
  bar: {
    baz: 42
  }
};

// Without optional chaining
const baz = foo.bar.baz; // Works fine

foo.bar = null;

// Without optional chaining
const baz = foo.bar.baz; // Throws a TypeError 🚨

In the above example, we have an interface called Foo, which contains a bar property that can either be an object with a baz property or null. If the bar property is null, attempting to access baz directly without the ?. operator will result in a TypeError. 😱

How does the ?. operator work?

Using the ?. operator is simple. Instead of directly accessing nested properties or methods, you can chain them together and append ?. after each dot.

const baz = foo?.bar?.baz;

In the example above, foo?.bar?.baz will return the value of baz if all the intermediate values (foo, bar, and baz) are not null or undefined. If any of them are null or undefined, the expression will short-circuit and evaluate to undefined without throwing an error. Isn't that neat? 😎

Is there a more common name for the ?. operator?

You might be wondering if there's a more commonly used name for the ?. operator. Well, you're not alone! It's indeed quite challenging to find information about it by searching for the operator itself. 🤔

Fear not! We got you covered. The ?. operator is most commonly referred to as the safe navigation operator. 🧭 This name accurately communicates its purpose of safely navigating through nested properties or methods.

Wrapping up

With the ?. operator (a.k.a. the safe navigation operator) in TypeScript, you can finally bid farewell to those annoying TypeError issues caused by accessing properties or methods on null or undefined values. 🙌

So go ahead and start implementing the ?. operator in your TypeScript projects to make your code much more robust and error-free! 💪 And if you encounter any issues or have any questions along the way, feel free to comment below. Let's help each other out! 🤝


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello