Skip to main content

no-unsafe-unary-minus

Require unary negation to take a number.

💭

This rule requires type information to run, which comes with performance tradeoffs.

TypeScript does not prevent you from putting a minus sign before things other than numbers:

const s = 'hello';
const x = -s; // x is NaN

This rule restricts the unary - operator to number | bigint.

eslint.config.mjs
export default tseslint.config({
rules: {
"@typescript-eslint/no-unsafe-unary-minus": "error"
}
});

Try this rule in the playground ↗

Examples

declare const a: string;
-a;

declare const b: {};
-b;
Open in Playground

Options

This rule is not configurable.

When Not To Use It

Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting.

See Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.

Resources

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy