restrict-plus-operands
要求加法的两个操作数为相同类型,并且为
bigint
、number
或string
.
该规则需要 类型信息 才能运行,但这会带来性能方面的权衡。
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
.
- 扁平配置
- 旧版配置
export default tseslint.config({
rules: {
"@typescript-eslint/restrict-plus-operands": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/restrict-plus-operands": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
- ❌ 错误
- ✅ 正确
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 = [
{
/** 是否允许 `any` 类型的值。 */
allowAny?: boolean;
/** 是否允许 `boolean` 类型的值。 */
allowBoolean?: boolean;
/** 是否允许潜在的 `null` 或 `undefined` 类型的值。 */
allowNullish?: boolean;
/** 是否允许将 `bigint`/`number` 类型的值和 `string` 类型的值相加。 */
allowNumberAndString?: boolean;
/** 是否允许 `regexp` 类型的值。 */
allowRegExp?: boolean;
/** 是否跳过复合赋值,例如 `+=`。 */
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
是否允许 any
类型的值。 Default: true
.
带有 { 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
是否允许 boolean
类型的值。 Default: true
.
带有 { 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
是否允许潜在的 null
或 undefined
类型的值。 Default: true
.
带有 { 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
是否允许将 bigint
/number
类型的值和 string
类型的值相加。 Default: true
.
带有 { 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;
let fn = (a: bigint, b: string) => a + b;
Open in PlaygroundallowRegExp
是否允许 regexp
类型的值。 Default: true
.
带有 { 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
是否跳过复合赋值,例如 +=
。 Default: false
.
带有 { skipCompoundAssignments: false }
的此规则的代码示例:
¥Examples of code for this rule with { skipCompoundAssignments: false }
:
- ❌ 错误
- ✅ 正确
let foo: bigint = 0n;
foo += 1;
let bar: number[] = [1];
bar += 1;
Open in Playgroundlet foo: bigint = 0n;
foo += 1n;
let bar: number = 1;
bar += 1;
Open in Playground何时不使用它
¥When Not To Use It
如果你不介意值中存在 "[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.
相关
¥Related To
进一步阅读
¥Further Reading
Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.
See Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.