strict-boolean-expressions: Add 'allow-null-union', 'allow-undefined-union', 'allow-string', and 'allow-number' options #2033
strict-boolean-expressions: Add 'allow-null-union', 'allow-undefined-union', 'allow-string', and 'allow-number' options #2033
Conversation
…union', 'allow-string', and 'allow-number' options
Added an |
@@ -178,6 +178,11 @@ export function isTypeFlagSet(type: ts.Type, flagToCheck: ts.TypeFlags): boolean | |||
/* tslint:enable:no-bitwise */ | |||
} | |||
|
|||
/** Type predicate to test for a union type. */ | |||
export function isUnionType(type: ts.Type): type is ts.UnionType { |
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.
shouldn't clutter utils with wrappers for isTypeFlagSet
. I can imagine this getting out of control
enum: [OPTION_ALLOW_NULL_UNION, OPTION_ALLOW_UNDEFINED_UNION, OPTION_ALLOW_STRING, OPTION_ALLOW_NUMBER], | ||
}, | ||
minLength: 0, | ||
maxLength: 4, |
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.
maxLength: 5
declare function get<T>(): T; | ||
|
||
if (get<string | number>()) {} | ||
~~~~~~~~~~~~~~~~~~~~~~ [If statement condition may be falsy if it is 0. Expected a boolean, undefined-union, or string.] |
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.
This error is unclear. How about This type is not allowed in the 'if' condition because it could be a number. Allowed types are boolean, undefined-union, or string
. You might have to modify it to make the string easier to generate
if (get<string>()) {} | ||
|
||
if (get<boolean | string>()) {} | ||
~~~~~~~~~~~~~~~~~~~~~~~ [If statement condition mixes multiple kinds of falsy values. Expected a boolean or string.] |
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.
`This type is not allowed in the 'if' condition because it unions one or more disallowed truthy/falsy types. Allowed types are boolean or string
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.
Changed "one or more" to "more than one" since it allows having exactly one falsy type.
5217471
to
5d41aa0
Compare
I like the idea of adding more options to 'strict-boolean-expressions'. Thank you for working on this. I haven't started yet, but I was planning to add a new option (ex. no-enum-boolean-coercion) to 'strict-boolean-expressions' because our team wants to enable the rule only for enum values first. It is risky because enum values start from 0 by default. On the other hand, we want to allow number-boolean-coercion if we're using enum as flags. (Ref: https://basarat.gitbooks.io/typescript/content/docs/enums.html#enums-as-flags ) Ref: #1555 Did you consider having options like:
|
So, if you want to be as permissive as possible, just set all the flags. This will still be less permissive than TypeScript as Adding an |
nice, thanks @andy-hanson! |
PR checklist
What changes did you make?
Added new options to
strict-boolean-expressions
to make it not quite as strict.TypeScript allows anything in an if-statement, so there's a lot of room in between that and requiring that everything be a boolean.
Is there anything you'd like reviewers to focus on?
This rule wouldn't allow you to use
string | number
as a condition even if bothallow-string
andallow-number
are on. I think that's fine.It will allow
number | undefined
ifallow-undefined
is on, since that containsundefined
in the union. But it's probably dangerous if the user just wanted to test forundefined
. Thoughts?