quick-lint-js

Find bugs in JavaScript programs.

E0718: using a '.' after a '?.' might fail, since '?.' might return 'undefined'

In JavaScript, x?.y ignores y and returns undefined if x is null or undefined. If ?. returns undefined, then using . after will throw an error:

let bug = { milestone: null  };
console.log(bug.milestone);               // null
console.log(bug.milestone?.name);         // undefined
console.log(bug.milestone?.name.trim());  // throws an error

Replacing the . with ?. will prevent the errors from being thrown at runtime:

let bug = { milestone: null  };
console.log(bug.milestone?.name?.trim());  // undefined

Documentation for other errors