-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Disallow expressions of type void
to be used in truthiness checks
#26234
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
Conversation
@typescript-bot test this |
Heya @RyanCavanaugh, I've started to run the extended test suite on this PR at 14d3c69. You can monitor the build here. It should now contribute to this PR's status checks. |
@weswigham is it possible to view the RWC diffs? |
Not easily (or without doubling the build time). I think I could probably surface them as an extra vsts pr without too much consternation - I'll look into it. Although we should also strive for usually having a clean RWC run, too. ❤️ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about the condition in a ConditionalExpresson a ? b : c
, the LHS of a && b
and a || b
and the operand of prefix unary negation !a
?
New RWC failures: https://github.com/firebase/firebase-js-sdk/blob/master/packages/database/src/core/util/Tree.ts#L156 - this function accepts an VS Code has a similar issue: https://github.com/Microsoft/vscode/blob/master/src/vs/base/parts/tree/browser/treeDefaults.ts#L268 |
@typescript-bot test this |
Heya @weswigham, I've started to run the extended test suite on this PR at 14d3c69. You can monitor the build here. It should now contribute to this PR's status checks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also include this check in conditional expressions.
src/compiler/checker.ts
Outdated
checkSourceElement(node.statement); | ||
} | ||
|
||
function checkTruthinessExpression(node: Expression) { | ||
const type = checkExpression(node); | ||
if (type === voidType) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A better way to write this is if (type.flags & TypeFlags.Void)
as we may at some point introduce other type instances that represent a void
type.
@typescript-bot test this |
Heya @weswigham, I've started to run the extended test suite on this PR at ca10b7a. You can monitor the build here. It should now contribute to this PR's status checks. |
const noop = () => { };
const blah = () => 5;
var fn = Math.random() > 0.5 ? noop : blah;
if (fn()) {
} Due to subtype reduction, RWC also found some code in VS Code (since fixed) where they were writing assert.ok(fs.exists("foo")); where |
@typescript-bot test this |
Heya @weswigham, I've started to run the extended test suite on this PR at ca10b7a. You can monitor the build here. It should now contribute to this PR's status checks. |
@RyanCavanaugh I agree the lodash example isn't obviously broken, but it definitely is suspicious given that a function returning anything can be assigned to a function returning |
I don't think this should have been done. A lot of times people use before: let timesTwo = (x: number) => {
console.log(`original: ${x}`);
return x * 2;
} after: let timesTwo = (x: number) => console.log(`original: ${x}`) || x * 2; another example: let timesTwo = (x: number) => assert(!Number.isNaN(x) && Number.isFinite(x), `Must provide a finite number.`) || x * 2 because
|
That looks obfuscated, not simplified... if you really want to do something like that that still clearly shows the side-effect nature of your call, you should use the comma operator. let timesTwo = (x: number) => (console.log(`original: ${x}`), x * 2); |
Let's take all discussion to #26262 |
Fixes an offhand comment from #17892
This found a bug (!) in
session.ts
-reloadFromFile
returnedvoid
so we were never hitting this line. I added return statements toreloadFromFile
based on what I think it should do but it's not immediately apparent.