-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Typescript should warn when checking for null or undefined on non-nullable type #17896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
As a quick fix for now there is a lint rule: https://palantir.github.io/tslint/rules/strict-type-predicates/ |
@Andy-MS Thanks for the quick reply! I didn't see in the description where it warns for this behavior, but if it does I can see it being quite useful. I think it should be baked into the language service itself being that it's less of a coding pattern and more of a coding contract feature like their example Update I got it to work using |
@Andy-MS since there is a working workaround, would you like me to close this issue? I'm not sure if there is a bigger issue about bringing important linting rules to TypeScript. I can leave it open and pivit the title if there isn't another issue covering this already. |
@RyanCavanaugh missed that one. closing. |
Proposal: Add an error or warning when
strictNullCheck
is turned on and a null check is applied to a non-null type.Motivation: The developer is often times converting a code base that does not originally have strict null checking turned on. After turning it on, bugs are often revealed and require refactoring, moving where the types are null and not null in the code. When refactoring, it is often times difficult to spot redundancy in null checks. By informing the user with an error or warning, it allows the developer to easily spot where their logic is flawed.
TypeScript Version: 2.4.0
Code
Expected behavior:
This should give warning that null check is not needed or should be moved outside the inner function. This would allow me to spot where my error is and remove the
!
operator with code like this:Obviously this is a very simple example to demonstrate my point, but this can get much more challenging with a very large and deeply nested code base.
Actual behavior:
No warning or error is displayed.
The text was updated successfully, but these errors were encountered: