Requires both operands of addition to have type number
or string
.
Examples of code for this rule:
var foo = '5.5' + 5;
var foo = 1n + 1;
var foo = parseInt('5.5', 10) + 10;
var foo = 1n + 1n;
The rule accepts an options object with the following properties:
type Options = {
// if true, check compound assignments (`+=`)
checkCompoundAssignments?: boolean;
// if true, 'any' itself and `string`,`bigint`, `number` is allowed.
allowAny?: boolean;
};
const defaults = {
checkCompoundAssignments: false,
allowAny: false,
};
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;
/*eslint @typescript-eslint/restrict-plus-operands: ["error", { "checkCompoundAssignments": true }]*/
let foo: number = 0;
foo += 1;
let bar = '';
bar += 'test';
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;
var fn = (a: any, b: any) => a + b;
var fn = (a: any, b: string) => a + b;
var fn = (a: any, b: bigint) => a + b;
var fn = (a: any, b: number) => a + b;
{
"@typescript-eslint/restrict-plus-operands": "error"
}
- TSLint: restrict-plus-operands