GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||
GeezerVison is showing github.com — Flag non-nullable functions in `if` statements as errors by jwbay · Pull Request #32802 · microsoft/TypeScript Conversation
Contributor
Under Example: function onlyErrorsWhenNonNullable(required: () => boolean, optional?: () => boolean) { if (required) { // now an error... } if (required()) { // ...because you probably meant this, which is still ok } if (optional) { // still ok } } Note the concern about third-party values coming in without a strictNullChecks context is still valid : import { ThirdPartyType, create } from 'some-package' const x: ThirdPartyType = create(); // this may now flag an error because the type definitions for some-package // don't mark someFunc as nullable when it should be if (x.someFunc) { } Should we make a recommendation here in the error message like casting to a nullable type?
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Member
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
|
There was an error while loading. Please reload this page.
|
Heya Ryan Cavanaugh (@RyanCavanaugh), I've started to run the parallelized community code test suite on this PR at 130615a. You can monitor the build here. It should now contribute to this PR's status checks. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Heya Ryan Cavanaugh (@RyanCavanaugh), I've started to run the parallelized Definitely Typed test suite on this PR at 130615a. You can monitor the build here. It should now contribute to this PR's status checks. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Heya Ryan Cavanaugh (@RyanCavanaugh), I've started to run the extended test suite on this PR at 130615a. You can monitor the build here. It should now contribute to this PR's status checks. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
There should probably be a special case for |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
The user suite test run you requested has finished and failed. I've opened a PR with the baseline diff from master. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I don't think this is the scope of strictNullChecks. This change injures the certainty and reliance of strictNullChecks. This change should be enabled by a new flag. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This didn't go nearly as well as expected, unfortunately. RWC turned up over a hundred examples of code of the form if (someExpr) { someExpr(); } where
Some of this also probably happened as codebases started out without However, there were a few hits some code that didn't call if (this.isComponentMounted) { ~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2774: This condition will always return true since the function is always defined. Did you mean to call it instead? this.setState({ isUnblocking: false }); } Further restricting the check to functions returning |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Let's try with the "functions returning boolean only" approach and hope for good results
Sorry, something went wrong.
There was an error while loading. Please reload this page.
3906a1e to
e81f5ec
Compare
Added a commit for this 🤞 I imagine one way to achieve a far better signal-to-noise ratio would be checking syntax to see if and how the thing being tested is used inside the block body. if (this.isComponentMounted) { // definitely suspicious because `isComponentMounted` isn't used in the block this.setState({ isUnblocking: false }); } if (this.isComponentMounted) { // but probably ok if it's called in the block this.setState({ isUnblocking: this.isComponentMounted() }); // or is passed to something else this.setState({ isUnblocking: someCheck(this.isComponentMounted) }); } However... I'm guessing a brute-force syntax walk over arbitrarily deep children would be too expensive to perform here. Is there some way to attach information like this during an earlier syntax walk, like I imagine might be done for scope checking or CFA?
This happens to already work due to the way we're checking specific syntax kinds. Added a test for it |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
e81f5ec to
327ff39
Compare
|
TypeScript Bot (@typescript-bot) test this |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Heya Ryan Cavanaugh (@RyanCavanaugh), I've started to run the extended test suite on this PR at 327ff39. You can monitor the build here. It should now contribute to this PR's status checks. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Heya Ryan Cavanaugh (@RyanCavanaugh), I've started to run the parallelized community code test suite on this PR at 327ff39. You can monitor the build here. It should now contribute to this PR's status checks. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Latest commit has one false positive (the event handler happened to return if (this.isComponentMounted) { ... } which is pretty good IMO. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
jwbay re: tree walks! We talked about this more and actually think a tree walk of the body of an
This would allow TS to check this for any function, not just those returning So if it's OK with you, we'd like to compare-and-contrast the results from the boolean-test PR with maybe a fresh PR that does do the walk? Would you be game to implement both ways so we can see which has the best yield? Thanks again for your work on this so far; I think this is going to be a high-value check either way. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I agree with the new approach #33178. This approach should be adopted. However, I still this is not null checks. This check should be called like |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Closing as #33178 is now merged |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
can't under stand why |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.