Skip to main content

restrict-plus-operands

Require both operands of addition to have type number or string.

Attributes

  • Included in configs
    • ✅ Recommended
    • 🔒 Strict
  • Fixable
    • 🔧 Automated Fixer
    • 💡 Suggestion Fixer
  • 💭 Requires type information

Rule Details

Examples of code for this rule:

var foo = '5.5' + 5;
var foo = 1n + 1;
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/restrict-plus-operands": "error"
}
};

Options

This rule accepts an options object with the following properties:

interface Options {
/**
* Whether to check compound assignments such as `+=`.
*/
checkCompoundAssignments?: boolean;
/**
* Whether to allow `any` typed values.
*/
allowAny?: boolean;
}

const defaultOptions: Options = [
{ checkCompoundAssignments: false, allowAny: false },
];

checkCompoundAssignments

Examples of code for this rule with { checkCompoundAssignments: true }:

/*eslint @typescript-eslint/restrict-plus-operands: ["error", { "checkCompoundAssignments": true }]*/

let foo: string | undefined;
foo += 'some data';

let bar: string = '';
bar += 0;

allowAny

Examples of code for this rule with { allowAny: true }:

var fn = (a: any, b: boolean) => a + b;
var fn = (a: any, b: []) => a + b;
var fn = (a: any, b: {}) => a + b;

How to Use

Resources