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
子句。
¥This rule reports when a +
operation combines two values of different types, or a type that is not bigint
, number
, or string
.
- Flat Config
- Legacy Config
export default tseslint.config({
rules: {
"@typescript-eslint/restrict-plus-operands": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/restrict-plus-operands": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
- ❌ Incorrect
- ✅ Correct
let foo = 1n + 1;
let fn = (a: string, b: never) => a + b;
Open in Playgroundlet foo = 1n + 1n;
let fn = (a: string, b: string) => a + b;
Open in Playground选项
This rule accepts the following options, and has more strict settings in the strict and strict-type-checked configs.
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 defaultOptionsRecommended: Options = [
{
allowAny: true,
allowBoolean: true,
allowNullish: true,
allowNumberAndString: true,
allowRegExp: true,
skipCompoundAssignments: false,
},
];
// These options are merged on top of the recommended defaults
const defaultOptionsStrict: Options = [
{
allowAny: false,
allowBoolean: false,
allowNullish: false,
allowNumberAndString: false,
allowRegExp: false,
},
];
¥Options
我们通常不建议使用这些选项,因为它们限制了可以检查哪些不正确的 +
使用类型。这反过来又严重限制了规则为确保生成的字符串和数字正确而进行的验证。
¥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 以避免需要
+
值。¥Using variadic forms of logging APIs to avoid needing to
+
values.console.log('The result is ' + true);
console.log('The result is', true); -
使用
.toFixed()
将数字强制转换为格式良好的字符串表示:¥Using
.toFixed()
to coerce numbers to well-formed string representations:const number = 1.123456789;
const result = 'The number is ' + number.toFixed(2);
// result === 'The number is 1.12' -
在其他类型上调用
.toString()
以标记显式和有意的字符串强制:¥Calling
.toString()
on other types to mark explicit and intentional string coercion: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
Whether to allow any
typed values. Default: true
.
带有 { allowAny: true }
的此规则的代码示例:
¥Examples of code for this rule with { allowAny: true }
:
- ❌ Incorrect
- ✅ Correct
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 Playground