Skip to content

noUnsafeUnaryMinus (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noUnsafeUnaryMinus": "error"
}
}
}
}

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

Unary negation converts any other operand to a number first. Negating a string, boolean, nullish value, or object usually produces NaN or an unexpected number, and TypeScript accepts many of these cases without reporting an error.

Operands typed as any or never are allowed. Explicit unknown operands are reported.

invalid-string.ts
declare const value: string;
-value;
/invalid-string.ts:2:1 lint/nursery/noUnsafeUnaryMinus ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ The operand of unary - may be string instead of number or bigint.

1 │ declare const value: string;
> 2 │ -value;
│ ^^^^^^
3 │

ℹ Unary - converts other values to a number first, which usually produces NaN or an unexpected result.

ℹ Convert the operand to a number explicitly, or negate a numeric value instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

invalid-object.ts
declare const value: { x: number };
-value;
/invalid-object.ts:2:1 lint/nursery/noUnsafeUnaryMinus ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ The operand of unary - may be object instead of number or bigint.

1 │ declare const value: { x: number };
> 2 │ -value;
│ ^^^^^^
3 │

ℹ Unary - converts other values to a number first, which usually produces NaN or an unexpected result.

ℹ Convert the operand to a number explicitly, or negate a numeric value instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

invalid-unknown.ts
declare const value: unknown;
-value;
/invalid-unknown.ts:2:1 lint/nursery/noUnsafeUnaryMinus ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ The operand of unary - may be unknown instead of number or bigint.

1 │ declare const value: unknown;
> 2 │ -value;
│ ^^^^^^
3 │

ℹ Unary - converts other values to a number first, which usually produces NaN or an unexpected result.

ℹ Convert the operand to a number explicitly, or negate a numeric value instead.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

valid-number.ts
declare const value: number;
-value;
valid-numeric-union.ts
declare const value: number | bigint;
-value;
valid-member.ts
declare const value: { x: number };
-value.x;