The optional chaining operator ?.
allows to access a deeply nested property, returning undefined
if the property or any
intermediate object is undefined
.
This usually means that the expression is expected to evaluate as undefined
in some cases. Therefore, using the optional chaining
operator in a context where returning undefined
throws an error can lead to runtime exceptions.
(event?.callback)(); // Noncompliant: when 'event' does not have 'callback' property TypeError is thrown
const { code } = event?.error; // Noncompliant: when 'event' does not have 'error' property TypeError is thrown
func(...event?.values); // Noncompliant: when 'event' does not have 'values' property TypeError is thrown
Since optional chaining represents multiple execution branches, having an error thrown in such a context can be hard to debug.