no-unsafe-unary-minus
Require unary negation to take a number.
💭
该规则需要 类型信息 才能运行。
TypeScript 不会阻止你在数字以外的内容前添加减号:
英:TypeScript does not prevent you from putting a minus sign before things other than numbers:
const s = 'hello';
const x = -s; // x is NaN
该规则将一元 -
运算符限制为 number | bigint
。
英:This rule restricts the unary -
operator to number | bigint
.
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-unsafe-unary-minus": "error"
}
};
示例
❌ 不正确
declare const a: string;
-a;
declare const b: {};
-b;
✅ 正确
-42;
-42n;
declare const a: number;
-a;
declare const b: number;
-b;
declare const c: number | bigint;
-c;
declare const d: any;
-d;
declare const e: 1 | 2;
-e;
选项
该规则不可配置。