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
[strict-boolean-expressions] option to allow checks against non-falsey types (i.e. non string / number / boolean) #1025
Comments
Strings are different though because function foo(arg: string | undefined | null) {
if (arg) {
// never gets reached because '' == false
}
}
foo(''); Which makes a boolean check on a string (OR a number) type inherently unsafe. |
I know empty strings for example are still seen as false which might be something different from what the user intended. In that case I'm with you as it should be a more specific check from the user. Examples: This should cause an error as it might do unintended things (string '' returns undefined instead of 0): function foo(text: string | undefined): number | undefined {
return text && text.length
} But this should work with this rule in my opinion: function foo(regex: RegExp | undefined): string | undefined {
return regex && regex.flags;
} |
definitely could add an option to allow objects, as that's always safe. Eg: // { allowObjects: true, allowNullable: true }
const stringOrUndefined =
foo.bar && // no error
foo.bar.bla; // error - string in a boolean
if (foo.bar) { // no error
// …
} |
line 4 in the last example should only create an error when |
correct! but ofc this would still error: // { allowObjects: true, allowNullable: true, ignoreRhs: true }
const stringOrUndefined =
foo.bar && // no error
foo.bar.bla; // no error
if (foo.bar) { // no error
// …
}
if (stringOrUndefined) { // error - string in a boolean location
// …
} |
I don't think that it should just allow objects, but all unions with types that can't be falsy (objects, symbols and functions). So maybe something like Also, IMO that should be default behavior when |
Definitely! I just couldn't think of the other non-falsey types at the time. I kind of wish I hadn't allowed |
I don't really understand why it is unsafe, as long as
I think that's really unnecessary, what would this rule even check in that case? I see 2 different use cases for
Are there really any other cases where you'd want more detailed control? Also, I think making |
It's safe as long as you assume that the "did not set" case is exactly the same as the I.e. I've seen react components with a boolean flag which is assumed to have a default value of true without actually setting a default value (an easy thing to forget). function foo(showFoo?: boolean) {
if (!showFoo) {
// ...
return 'bar';
}
return 'foo';
}
foo(true); // 'foo'
foo(false); // 'bar'
foo(); // 'bar' - forgot the default value, so this evaluates to bar function fooSafe(showFoo?: boolean) {
if (showFoo === false) { // this causes the unset case to be treated the same as the true case.
// ...
return 'bar';
}
return 'foo';
}
foo(true); // 'foo'
foo(false); // 'bar'
foo(); // 'foo' - explicit boolean check means that the omission of the default value doesn't matter
// this would have the same effect
// function fooSafe(showFoo: boolean = true) There's a reason that flow in strict mode includes compiler errors for nullable strings, nullable numbers and nullable booleans - to make sure you can never mess this case up.
Definitely is necessary - I wouldn't add them myself. But I would be comfortable accepting PRs for the options if they were all named I can see an argument that people do explicitly want to allow empty strings to be treated as falsey. ESP when user input comes into play there is a strong argument for empty string being falsey - but it requires a lot of careful planning from all parts of a system. I don't have an argument for consistently treating 0 as falsey though. Maybe there is one, but I don't have one.
Definitely would consider that for a 3.0 release (if this option got added before then). |
I added With nullish coalescing we can use |
I'm indifferent on the "allow safe null check" option. I think |
While I honestly dont see why this |
Well, after giving it a minute of thought I do realize that my suggestion would make the name "strict-boolean-expressions" be a bit misleading. But perhaps the issue is with the name of the rule then? Perhaps it should be called "no-unsafe-boolean-coercion" or something to that effect? Because as far as I can see, that is the reason to be using this rule. |
I'm happy to have an option. Though I would prefer I think it's probably better for this rule to be "fully strict by default", with the option of relaxing it where you're okay having "a little less explicitness and strictness". this project is driven by the community. Nobody has actioned it because nobody has been driven enough to implement it (this issue is sitting at 0 reacts on the OP, which is good signal that it's not high on anyone's want/todo list). |
Repro
Expected Result
There shouldn't be an error as the option
allowNullable
states to include undefined.#698 asked for a quick null / undefined check which is not the case yet.
Actual Result
Additional Info
Currently I assume
allowNullable
only allows fortrue | false | undefined | null
and does not allow anything else (which would beFoo
in this case). This is useful for something likeproperty?: boolean
which can betrue | false | undefined
and is still allowed by this rule but is not helpful for quick null / undefined checks.Versions
@typescript-eslint/eslint-plugin
2.3.1
@typescript-eslint/parser
2.3.1
TypeScript
3.6.3
ESLint
6.5.0
node
12.10.0
npm
6.11.3
The text was updated successfully, but these errors were encountered: