Skip to content
← Back to rules

typescript/no-unsafe-unary-minus Correctness

✅ This rule is turned on by default when type-aware linting is enabled.
💭 This rule requires type information.

What it does ​

This rule disallows using the unary minus operator on a value which is not of type 'number' | 'bigint'.

Why is this bad? ​

The unary minus operator should only be used on numeric values. Using it on other types can lead to unexpected behavior due to JavaScript's type coercion rules.

Examples ​

Examples of incorrect code for this rule:

ts
declare const value: any;
const result1 = -value; // unsafe on any

declare const str: string;
const result2 = -str; // unsafe on string

declare const bool: boolean;
const result3 = -bool; // unsafe on boolean

declare const obj: object;
const result4 = -obj; // unsafe on object

declare const arr: any[];
const result5 = -arr; // unsafe on array

Examples of correct code for this rule:

ts
declare const num: number;
const result1 = -num; // safe

declare const bigint: bigint;
const result2 = -bigint; // safe

const literal = -42; // safe

const bigintLiteral = -42n; // safe

declare const union: number | bigint;
const result3 = -union; // safe

// Convert to number first if needed
declare const str: string;
const result4 = -Number(str); // safe conversion

How to use ​

To enable this rule using the config file or in the CLI, you can use:

json
{
  "options": {
    "typeAware": true
  },
  "rules": {
    "typescript/no-unsafe-unary-minus": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  options: { typeAware: true },
  rules: {
    "typescript/no-unsafe-unary-minus": "error",
  },
});
bash
oxlint --type-aware --deny typescript/no-unsafe-unary-minus

Version ​

This rule was added in v1.12.0.

References ​