no-unsafe-unary-minus
Require unary negation to take a number.
✅
Extending "plugin:@typescript-eslint/recommended-type-checked"
in an ESLint configuration enables this rule.
💭
This rule requires type information to run.
TypeScript does not prevent you from putting a minus sign before things other than numbers:
const s = 'hello';
const x = -s; // x is NaN
This rule restricts the unary -
operator to number | bigint
.
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-unsafe-unary-minus": "error"
}
};
Try this rule in the playground ↗
Examples
- ❌ Incorrect
- ✅ Correct
declare const a: string;
-a;
declare const b: {};
-b;
Open in Playground-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;
Open in PlaygroundOptions
This rule is not configurable.
When Not To Use It
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 degredations after enabling type checked rules.