typescript-eslint: [@typescript-eslint/restrict-plus-operands] False positive for type `0|1`

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

{
  "rules": {
    "@typescript-eslint/restrict-plus-operands": ["error"]
  }
}
export function getFutureVersion(
  policy: number,
  newVersion: string,
  baseVersion?: string
): number[] {
  const toRelease: number[] = [];
  const baseRelease: number[] = [];
  return baseRelease.map((_, index) => {
    const toPart = toRelease[index] || 0;
    if (index < policy) {
      return toPart;
    }
    if (index === policy) {
      return toPart + (baseVersion === undefined ? 0 : 1);
    }
    return 0;
  });
}

happens after update from v5.12.1to v5.13.0.

Expected Result

Should not cause an error.

Actual Result image The rule causes a false positive on code above. https://github.com/renovatebot/renovate/pull/14597/files#diff-f6e993a4715dd7526818c5bb2cdfdaa3a139db2aa290ba8857b9d93f8fe03c4f

Additional Info

Versions

package version
@typescript-eslint/eslint-plugin 5.14.0
@typescript-eslint/parser 5.14.0
TypeScript 4.6.2
ESLint 8.10.0
node 14.19.0

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 24 (11 by maintainers)

Most upvoted comments

On the latest TS version, this doesn’t seem to be an error anymore.

So I simplified your repro further to this eslintrc https://github.com/bradzacher/typescript-eslint-issue-4689/blob/patch-1/.eslintrc.js

The interesting thing is that if you remove the lint rule '@typescript-eslint/no-misused-promises': ['error'], from the config, the error in restrict-template-expressions goes away!!!

Thanks for following up with more details @viceice! The issue in the playground code snippet is that toPart is type any. Confirmed that if we change the playground code snippet to const toPart: number there’s no more issue updated playground link. Your issue is with TypeScript, not typescript-eslint.

By the way, as a general tip, instead of posting screenshots of code or CI reports, please post plain text and/or links to playground snippets. Screenshots are harder for us to work with: they can’t be copy&pasted and aren’t accessible to folks who rely on screenreaders unless they have very detailed alt text / captions.