restrict-plus-operands
Require both operands of addition to be the same type and be
bigint
,number
, orstring
.
该规则需要 类型信息 才能运行。
TypeScript 允许 +
将任意类型的两个值相加。
但是,添加不同类型和/或不同基元类型的值通常是程序员错误的标志。
英:TypeScript allows +
adding together two values of any type(s).
However, adding values that are not the same type and/or are not the same primitive type is often a sign of programmer error.
此规则在 +
操作组合两个不同类型的值或非 bigint
、number
或 string
的类型时进行报告。
英:This rule reports when a +
operation combines two values of different types, or a type that is not bigint
, number
, or string
.
module.exports = {
"rules": {
"@typescript-eslint/restrict-plus-operands": "error"
}
};
示例
- ❌ 不正确
- ✅ 正确
let foo = '5.5' + 5;
let foo = 1n + 1;
Open in Playgroundlet foo = parseInt('5.5', 10) + 10;
let foo = 1n + 1n;
Open in Playground选项
我们通常建议不要使用这些选项,因为它们限制了可以检查的错误 +
用法的种类。
这反过来又严重限制了规则为确保生成的字符串和数字正确而进行的验证。
英:We generally recommend against using these options, as they limit which varieties of incorrect +
usage can be checked.
This in turn severely limits the validation that the rule can do to ensure that resulting strings and numbers are correct.
使用 allow*
选项的更安全替代方案包括:
英:Safer alternatives to using the allow*
options include:
- 使用可变形式的日志记录 API 以避免需要
+
值。console.log('The result is ' + true);
console.log('The result is', true); - 使用
.toFixed()
将数字强制转换为格式良好的字符串表示形式:const number = 1.123456789;
const result = 'The number is ' + number.toFixed(2);
// result === 'The number is 1.12' - 在其他类型上调用
.toString()
来标记显式且有意的字符串强制转换:const arg = '11';
const regex = /[0-9]/;
const result =
'The result of ' +
regex.toString() +
'.test("' +
arg +
'") is ' +
regex.test(arg).toString();
// result === 'The result of /[0-9]/.test("11") is true'
allowAny
此规则与 { allowAny: true }
的代码示例:
英:Examples of code for this rule with { allowAny: true }
:
- ❌ 不正确
- ✅ 正确
let fn = (a: number, b: []) => a + b;
let fn = (a: string, b: []) => a + b;
Open in Playgroundlet fn = (a: number, b: any) => a + b;
let fn = (a: string, b: any) => a + b;
Open in PlaygroundallowBoolean
此规则与 { allowBoolean: true }
的代码示例:
英:Examples of code for this rule with { allowBoolean: true }
:
- ❌ 不正确
- ✅ 正确
let fn = (a: number, b: unknown) => a + b;
let fn = (a: string, b: unknown) => a + b;
Open in Playgroundlet fn = (a: number, b: boolean) => a + b;
let fn = (a: string, b: boolean) => a + b;
Open in PlaygroundallowNullish
此规则与 { allowNullish: true }
的代码示例:
英:Examples of code for this rule with { allowNullish: true }
:
- ❌ 不正确
- ✅ 正确
let fn = (a: number, b: unknown) => a + b;
let fn = (a: number, b: never) => a + b;
let fn = (a: string, b: unknown) => a + b;
let fn = (a: string, b: never) => a + b;
Open in Playgroundlet fn = (a: number, b: undefined) => a + b;
let fn = (a: number, b: null) => a + b;
let fn = (a: string, b: undefined) => a + b;
let fn = (a: string, b: null) => a + b;
Open in PlaygroundallowNumberAndString
此规则与 { allowNumberAndString: true }
的代码示例:
英:Examples of code for this rule with { allowNumberAndString: true }
:
- ❌ 不正确
- ✅ 正确
let fn = (a: number, b: unknown) => a + b;
let fn = (a: number, b: never) => a + b;
Open in Playgroundlet fn = (a: number, b: string) => a + b;
let fn = (a: number, b: number | string) => a + b;
Open in PlaygroundallowRegExp
此规则与 { allowRegExp: true }
的代码示例:
英:Examples of code for this rule with { allowRegExp: true }
:
- ❌ 不正确
- ✅ 正确
let fn = (a: number, b: RegExp) => a + b;
Open in Playgroundlet fn = (a: string, b: RegExp) => a + b;
Open in PlaygroundskipCompoundAssignments
此规则与 { skipCompoundAssignments: false }
的代码示例:
英:Examples of code for this rule with { skipCompoundAssignments: false }
:
- ❌ 不正确
- ✅ 正确
let foo: string | undefined;
foo += 'some data';
let bar: string = '';
bar += 0;
Open in Playgroundlet foo: number = 0;
foo += 1;
let bar = '';
bar += 'test';
Open in Playground何时不使用它
如果你不介意值中存在 "[object Object]"
或不正确类型强制的风险,那么你将不需要此规则。
英:If you don't mind a risk of "[object Object]"
or incorrect type coercions in your values, then you will not need this rule.
相关
进一步阅读
选项
该规则接受以下选项
type Options = [
{
/** Whether to allow `any` typed values. */
allowAny?: boolean;
/** Whether to allow `boolean` typed values. */
allowBoolean?: boolean;
/** Whether to allow potentially `null` or `undefined` typed values. */
allowNullish?: boolean;
/** Whether to allow `bigint`/`number` typed values and `string` typed values to be added together. */
allowNumberAndString?: boolean;
/** Whether to allow `regexp` typed values. */
allowRegExp?: boolean;
/** Whether to skip compound assignments such as `+=`. */
skipCompoundAssignments?: boolean;
},
];
const defaultOptions: Options = [
{
allowAny: true,
allowBoolean: true,
allowNullish: true,
allowNumberAndString: true,
allowRegExp: true,
skipCompoundAssignments: false,
},
];