Rule Proposal: prefer-optional-chaining
See original GitHub issuePlease describe what the rule should do:
The new rule warns obj && obj.prop
-like notations to suggest obj?.prop
.
What new ECMAScript feature does this rule relate to?
Optional chaining
What category of rule is this? (place an “X” next to just one item)
[X] Suggests an alternate way of doing something (suggestion)
Provide 2-3 code examples that this rule will warn about:
//✖ BAD
obj && obj.prop
obj != null ? obj.prop : undefined
obj && obj[key]
obj != null ? obj[key] : undefined
func && func()
func != null ? func() : undefined
if (func) func()
if (func) { func() }
//✔ GOOD
obj?.prop
obj?.[key]
func?.()
Why should this rule be included in ESLint (instead of a plugin)?
obj && obj.prop
-like notation is a popular practice. This rule will help people to learn the new syntax Optional Chaining. This is about a language feature rather than third-party platforms or libraries.
Are you willing to submit a pull request to implement this rule?
Yes.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:61
- Comments:8 (4 by maintainers)
Top Results From Across the Web
prefer-optional-chain | typescript-eslint
Because the optional chain operator only chains when the property value is null or undefined , it is much safer than relying upon...
Read more >typescript-eslint/prefer-optional-chain.md at main - GitHub
Because the optional chain operator only chains when the property value is null or undefined , it is much safer than relying upon...
Read more >Prefer using concise optional chain expressions ... - GitLab
TypeScript 3.7 added support for the optional chain operator. This operator allows you to safely access properties and methods on objects when ...
Read more >Rule proposal: `prefer-optional-chaining` #429 - Issuehunt
There is a reason that the @typescript-eslint rule no longer has an autofix. It is not possible to have a rule that autofixes...
Read more >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 >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
I’d agree that the code definitely not good and that nobody should ever write it.
But people do, and people rely on this sort of behaviour.
https://github.com/typescript-eslint/typescript-eslint/issues/2963 https://github.com/typescript-eslint/typescript-eslint/issues/1893
And there are other problems as well, like
https://github.com/typescript-eslint/typescript-eslint/issues/1820
This code is much more reasonable, relying on
=== null
is a pretty common thing (though I always recommend== null
as it handles bothnull
andundefined
, and IMO the two should always be handled the same). Fixing this case will break code, and reporting here can be considered a false positive.What I’m saying is that without type information and complete, omnipresent knowledge of the usage of the value - this rule will always have false positives, and a fixer is not always safe.
Suggestion fixers are an alternative, but from my experience users will just apply them without thinking, and will break their code.
I just wanted to drop my learnings to save you guys from making the same mistakes.
friendly ping @mysticatea : are you still working on this? it seems very useful! 👍