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

declare const a: string;
-a;

declare const b: {};
-b;
Open in Playground

Options

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 Performance Troubleshooting if you experience performance degredations after enabling type checked rules.

Resources