Skip to content

[restrict-plus-operands] false positive on types like {} & string #2506

Closed
@papb

Description

@papb
  • 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"
  }
}
declare const a: {} & string;
declare const b: string;
const x = a + b; // Operands of '+' operation must either be both strings or both numbers. Consider using a template literal. (@typescript-eslint/restrict-plus-operands)

Expected Result

No error

Actual Result

Operands of '+' operation must either be both strings or both numbers. Consider using a template literal. (@typescript-eslint/restrict-plus-operands)

Additional Info

The minimal example above might seem nonsense, using {} & string, but it's to be minimal. I encountered a situation in real code involving complex type inferences in which one of the operands apparently ended up being inferred as string | (Buffer & string) (which also seems nonsense, maybe I should open an issue at TS too... Regardless, WhateverType & string is a string, so the linter shouldn't complain).

Versions

package version (attempt 1, with XO) version (attempt 2, minimal, without XO)
@typescript-eslint/eslint-plugin 3.10.1 4.0.1
@typescript-eslint/parser 3.10.1 4.0.1
TypeScript 4.0.2 4.0.2
ESLint 7.7.0 7.8.1
node 12.14.1 12.14.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions