eslint-plugin-unary-minus
TypeScript icon, indicating that this package has built-in type declarations

0.2.4 • Public • Published

eslint-plugin-unary-minus npm license Build

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; // OK
const g = (a: {}) => -a; // also OK

console.log(f("hi there")); // NaN
console.log(g({})); // NaN

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"
  }
}

Package Sidebar

Install

npm i eslint-plugin-unary-minus

Weekly Downloads

0

Version

0.2.4

License

MIT

Unpacked Size

14.2 kB

Total Files

23

Last publish

Collaborators

  • samestep