Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

strict-boolean-expressions: Allow 'any', and 'true' and 'false' literal types #2758

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/rules/strictBooleanExpressionsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,12 @@ function getTypeFailure(type: ts.Type, options: Options): TypeFailure | undefine

switch (triState(kind)) {
case true:
return TypeFailure.AlwaysTruthy;
// Allow 'any'. Allow 'true' itself, but not any other always-truthy type.
// tslint:disable-next-line no-bitwise
return Lint.isTypeFlagSet(type, ts.TypeFlags.Any | ts.TypeFlags.BooleanLiteral) ? undefined : TypeFailure.AlwaysTruthy;
case false:
return TypeFailure.AlwaysFalsy;
// Allow 'false' itself, but not any other always-falsy type
return Lint.isTypeFlagSet(type, ts.TypeFlags.BooleanLiteral) ? undefined : TypeFailure.AlwaysFalsy;
case undefined:
return undefined;
}
Expand Down
14 changes: 12 additions & 2 deletions test/rules/strict-boolean-expressions/default/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,10 @@ boolType ? strType : undefined;
!enumType;
~~~~~~~~ [This type is not allowed in the operand for the '!' operator because it is an enum. Only booleans are allowed.]
!!classType;
~~~~~~~~~~ [This type is not allowed in the operand for the '!' operator because it is always falsy. Only booleans are allowed.]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!classType is of type false, thus the removed error.

~~~~~~~~~ [This type is not allowed in the operand for the '!' operator because it is always truthy. Only booleans are allowed.]
!bwrapType;
~~~~~~~~~ [This type is not allowed in the operand for the '!' operator because it is always truthy. Only booleans are allowed.]
!!undefined;
~~~~~~~~~~ [This type is not allowed in the operand for the '!' operator because it is always truthy. Only booleans are allowed.]
~~~~~~~~~ [This type is not allowed in the operand for the '!' operator because it is always falsy. Only booleans are allowed.]

/*** Valid ***/
Expand Down Expand Up @@ -180,3 +178,15 @@ for (let j = 0; j; j++) { break; }
~ [This type is not allowed in the 'for' condition because it is a number. Only booleans are allowed.]
/*** Valid ***/
for (let j = 0; j > numType; j++) { break; }

// Allow always-truthy or always-falsy in the case of 'true' and 'false'
while (true) {}
if (false) {}
// Do *not* allow 'true' in a union
declare var trueOrDate: true | Date;
if (trueOrDate) {}
~~~~~~~~~~ [This type is not allowed in the 'if' condition because it is always truthy. Only booleans are allowed.]

declare var a: any;
// Allow 'any'
if (a) {}