@typescript-eslint/no-unsafe-unary-minus

Require the operand of unary - to be a number or a bigint

needs types

Also called typescript-eslint/no-unsafe-unary-minus, typescript/no-unsafe-unary-minus

-value converts value to a number first. For a string that isn't numeric, an object, or an array with several items, the result is NaN. TypeScript accepts it without an error.

A union is reported if any of its members isn't a number or a bigint. any isn't reported.

bun lint --type-aware turns this rule on in TypeScript files.

Configure#

Off by default. This rule needs types, so turning it on makes bun lint type check your code first.

bunfig.toml
[lint.rules]
"@typescript-eslint/no-unsafe-unary-minus" = "error" # or "warn"

Examples#

example.ts1 problem
function refund(amount: string) {
  return -amount;
}
example.ts1 problem
function flip(offset: number | string) {
  return -offset;
}
example.tsNo problems
function refund(amount: string) {
  return -Number(amount);
}

function flip(offset: number | bigint) {
  return -offset;
}