eslint-plugin-unary-minus
DEPRECATION NOTICE: This plugin is no longer necessary as of
typescript-eslint v6.14.0. Use the
@typescript-eslint/no-unsafe-unary-minus
rule instead of this package.
A typescript-eslint plugin restricting unary negation to numbers.
Requires TypeScript v5.1+.
As explained on Stack Overflow, TypeScript does not prevent you from
putting a minus sign before things other than numbers:
const f = (a: string) => -a;
const g = (a: {}) => -a;
console.log(f("hi there"));
console.log(g({}));
This ESLint plugin catches that sort of bug by checking that every usage of
unary negation has an argument with type assignable to number | bigint
.
Installation
You'll first need to install ESLint, TypeScript, and typescript-eslint:
npm i --save-dev eslint typescript @typescript-eslint/eslint-plugin
Next, install eslint-plugin-unary-minus
:
npm i --save-dev eslint-plugin-unary-minus
Usage
Add unary-minus
to the plugins section of your .eslintrc
configuration file.
You can omit the eslint-plugin-
prefix:
{
"plugins": ["unary-minus"]
}
Then add this rule under the rules section.
{
"rules": {
"unary-minus/only-numbers": "error"
}
}