Rule proposal: Disallow optional chaining on undeclared variables
See original GitHub issueIt’s unfortunate that ?.
is sugar for x !== undefined
and not typeof x !== 'undefined'
. The subtle difference is that the first one will fail if x
is not defined at all.
Would it make sense to disallow the operator unless the variable is in the scope?
Fail
iDontExist?.meNeither
Pass
globalThis.iDontExist?.meNeither
let iDontExist;
iDontExist?.meNeither
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:11 (7 by maintainers)
Top Results From Across the Web
no-unsafe-optional-chaining - Pluggable JavaScript Linter
This rule aims to detect some cases where the use of optional chaining doesn't prevent runtime errors. In particular, it flags optional chaining...
Read more >Optional chaining of unknown should be unknown #37700
Currently it is forbidden to use optional chaining on unknown . But for all possible types in JavaScript, optional chaining will return ...
Read more >Optional Chaining in JavaScript returns undefined instead of ...
An optional chain does not evaluate to the nullish value on which the property was accessed, but to undefined - just like you...
Read more >Optional chaining (?.) - JavaScript - MDN Web Docs - Mozilla
The optional chaining ( ?. ) operator accesses an object's property or calls a function. If the object accessed or function called is ......
Read more >Use JavaScript Optional Chaining, Today! - Abdelrahman Ismail
Optional Chaining is a new JavaScript API that will make developers' lives easier :D. Optional Chaining is currently at Stage 3, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ah, it can also be optional call,
no-optional-chaining-on-undeclared-variable
is better, if this rule also check optional call, and I think we should.no-undeclared-variable-optional-member
?