Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Bug: [strict-boolean-expressions] allowNullableBoolean - support array predicate functions #10688

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

Closed
4 tasks done
xkabylgSICKAG opened this issue Jan 20, 2025 · 5 comments Β· Fixed by #10698
Closed
4 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@xkabylgSICKAG
Copy link

xkabylgSICKAG commented Jan 20, 2025

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=5.7.2&fileType=.tsx&code=LAKAxg9gdgzgLgAgIYC4EAoCuUAmBTAMwEso8cEAfBAIwggBs8kpKEpN76BKAbQF0EAXgQ84AJ0x4ANAmz5ipHDPac%2BAblBIAdDAgBbPOnQAPLkIB8CUxpChIsRNTTpaDJiyoru-ISPGTlDnp1UGodfUMTM0FLa1A7aHgEMGdXRmZWOUISMl4BYVEJaVlcbMUQ8HCDI1MLKy4bBIcEHFS6dKg830LJPlAcKsjamPqgA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUFs6gAQBcBPABxQGNoBLEggWhXioDsCB6ZA68%2BgIwHt%2BSAIbMGADxLQUyKv2aoMAbXC4IkRNGj9okADSq1OYFGHx4-AO4A5BPGG8kAIUEjm6MF1iIwAX0NGALqq-r5AA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

const a: (undefined | boolean | null)[] = [true, undefined, null];
a.some((x) => x);

ESLint Config

module.exports = {
  "rules": {
          "@typescript-eslint/strict-boolean-expressions": [
        "error",
            { "allowNullableBoolean": true }
          ]
  }
}

tsconfig

{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

Expected Result

I expected there to be no errors.

Actual Result

It seems like the array functions doesn't respect the "allowNullableBoolean" option

Additional Info

Probably introduced in this PR #10106

@xkabylgSICKAG xkabylgSICKAG added bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Jan 20, 2025
@bradzacher bradzacher changed the title Bug: [strict-boolean-expressions] allowNullableBoolean doesn't work with array functions Enhancement: [strict-boolean-expressions] support array predicate functions Jan 20, 2025
@bradzacher bradzacher changed the title Enhancement: [strict-boolean-expressions] support array predicate functions Bug: [strict-boolean-expressions] allowNullableBoolean - support array predicate functions Jan 20, 2025
@bradzacher bradzacher added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels Jan 20, 2025
@fuadsaud
Copy link

This is useful for logic expressions that use null coalescing, e.g.:

arr.filter(x =>
  x.y > 42 && x?.z > 0
)

@kirkwaiblinger
Copy link
Member

Just noting for conversation - this isn't a bug about the allowNullableBoolean specifically. It's just that the array predicate method check doesn't respect the options at all (and always demands a boolean from array predicates). It probably does make sense to respect the options (though boolean really is better in these cases since it makes predicates eligible for inferred type predicates!)

@fuadsaud
Copy link

Thanks, @kirkwaiblinger, that makes sense!

Could you clarify what you mean by

though boolean really is better in these cases since it makes predicates eligible for inferred type predicates!

I'm not sure I follow.

@kirkwaiblinger
Copy link
Member

Could you clarify what you mean by

though boolean really is better in these cases since it makes predicates eligible for inferred type predicates!

I'm not sure I follow.

Oh, yeah, basically I'm just saying:

(playground)

const arrayOfNullables = [null, "not null"];

const nonNullablesBad = arrayOfNullables.filter(x => x);
// TS ERROR - x is possibly null
nonNullablesBad.forEach(x => console.log(x.toLowerCase()));

const nonNullablesGood = arrayOfNullables.filter(x => x != null);
// works!
nonNullablesGood.forEach(x => console.log(x.toLowerCase()));

For further reading on inferred type predicates, see the TS 5.5 release notes. Also related, #9764


So, IMO there's additional benefit in TS to using an actual boolean return in array predicate callbacks compared to the ordinary stylistic reasons for using strict-boolean-expressions.

@fuadsaud
Copy link

Thanks! I was not familiar with inferred type predicates, makes sense.

@github-actions github-actions bot added the locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. label Mar 4, 2025
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 4, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
4 participants