Skip to main content

no-unsafe-unary-minus

Require unary negation to take a number.

💭

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

declare const a: string;
-a;

declare const b: {};
-b;

✅ Correct

-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;

Options

This rule is not configurable.

Resources