Comments (2)
If you want to catch this case, use no-unnecessary-type-assertion
from typescript-eslint.
This is working as expected!
In the error case you have an expression that may return undefined
- so by using a non-null assertion you are cancelling the undefined and creating a bug vector in the code.
However with the continued chain case the optional chain short-circuits - meaning you may never reach the non-null assertion at all.
Specifically when the non-null assertion is directly chained off it does not apply to the entire expression - instead it applies just to the property it is directly attached to.
I.e. In your "bad" case the non-null assertion is not removing the undefined from the optional chain - instead it's removing the nullishness from the b
property.
This is obviously still a bug vector - non-nullish assertions are never truly safe - but it's not a "this is always completely wrong" usage like the former. If you want to prevent this case you can use the no-non-null-assertion
lint rule.
from typescript-eslint.
Related Issues (20)
- Bug: Parsing error: ESLint was configured to run on <file> using `parserOptions.project` HOT 1
- Docs: Unusually large padding after table cell lines in Contributor_Tiers.mdx
- Bug: @typescript-eslint/no-unnecessary-condition false positives for getters HOT 8
- Bug: [@typescript-eslint/no-invalid-void-type] false positive when used in a return type where there is a union HOT 1
- Bug: [explicit-function-return-type] false negatives on inner function when using allowTypedFunctionExpressions option
- Bug: [dot-notation] False positive with optional chaining and allowPrivateClassPropertyAccess: true
- Docs: Prioritize formatting rules over other, less-important ones in docs HOT 8
- Bug: [no-unnecessary-type-assertion] (regression) false positives on template literals with no expressions
- Error: Plugin "@typescript-eslint" was conflicted HOT 1
- Docs: Mention stylistic(TypeChecked) at end of 'getting started' docs? HOT 2
- Bug: [consistent-type-assertions] broken autofix for arrow functions
- Configs: Enable no-undef by default HOT 6
- Bug: [no-unnecessary-type-assertion] assumes better type narrowing than typescript offers HOT 3
- Bug(website): Rules table filters neglect some rules
- Bug: `tsconfig.json` not respected in new files when multiple tsconfigs include the same directory with `useProjectService` enabled HOT 1
- Bug: incompatible type `ParserOptions['projectFolderIgnoreList']`
- Bug: ESLint raises type-safety warnings for imported functions and types from shared libraries HOT 4
- Docs: remove import from config file in getting started HOT 1
- Bug: [await-thenable] should check `for-await` loop HOT 3
- Docs: Some additional details around contributors/committers
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript-eslint.