Skip to main content

When adding two variables, operands must both be of type number or of type string (restrict-plus-operands)

Rule Details

Examples of code for this rule:

var foo = '5.5' + 5;
var foo = 1n + 1;

Options

This rule has an object option:

  • "checkCompoundAssignments": false: (default) does not check compound assignments (+=)
  • "checkCompoundAssignments": true

checkCompoundAssignments

Examples of code for the { "checkCompoundAssignments": true } option:

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

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

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

How to use

{
"@typescript-eslint/restrict-plus-operands": "error"
}

Compatibility

Attributes

  • โœ… Recommended
  • ๐Ÿ”ง Fixable
  • ๐Ÿ’ญ Requires type information