npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

eslint-plugin-safe-number

v1.1.1

Published

ESLint rule to prevent unsafe Number conversions

Readme

eslint-plugin-safe-number

An ESLint plugin to prevent unsafe conversions to Number() from null or undefined values.

npm version License: MIT

Installation

npm install --save-dev eslint-plugin-safe-number

⚠️ Requirements

This plugin works ONLY in TypeScript projects.

It relies on the TypeScript Type Checker to determine if a variable is nullable. It will not work in standard JavaScript projects or without proper parserOptions.

Configuration

module.exports = {
  // ...
  parser: "@typescript-eslint/parser",
  parserOptions: {
    ecmaVersion: "latest",
    sourceType: "module",
    // ⚠️ CRITICAL: Point to your TSConfig(s) to enable type-aware linting
    project: ["./packages/*/tsconfig.json", "./tsconfig.json"],
    tsconfigRootDir: __dirname,
  },
  plugins: ["safe-number"],
  rules: {
    // ... other rules
    "safe-number/no-unsafe-number-conversion": "error",
  },
};

❌ The Problem

In JavaScript/TypeScript, passing null to the Number() constructor results in 0. This is often a source of subtle bugs in data processing, financial calculations, or optional form fields where "no value" should not be treated as "zero".

Number(null); // Result: 0  (Often unexpected)
Number(undefined); // Result: NaN
Array.from(["1", null], Number); // Unsafe!
getMaybeNullValue().then(Number); // Unsafe!

🔍 Rule Behavior & Auto-Fixes

The no-unsafe-number-conversion rule analyzes the TypeScript type of the value being passed to Number() and offers Suggestions (Quick Fixes) depending on the exact nullability of the value. The goal is to prevent unsafe numeric conversions such as Number(null) or Number(undefined).

1. Strict Null (T | null)

If the variable type is exactly Type | null:

❌ Incorrect

const val: string | null = null;
Number(val);

✅ Fixed (Suggestion)

val !== null ? Number(val) : null;

2. Strict Undefined (T | undefined)

If the variable type is exactly Type | undefined:

❌ Incorrect

const val: string | undefined = undefined;
Number(val);

✅ Fixed (Suggestion)

val !== undefined ? Number(val) : undefined;

3. Mixed Types (T | null | undefined)

If the variable can be null or undefined in addition to the base type:

❌ Incorrect

declare const val: string | null | undefined;
Number(val);

✅ Fixed (Suggestion)

val !== null && val !== undefined ? Number(val) : val;

4. Array Callbacks (.map, .forEach, etc.)

The rule also detects unsafe conversions in array callbacks such as map(Number):

❌ Incorrect

const arr: (string | null)[] = ["1", null];
arr.map(Number);

✅ Fixed (Suggestion)

arr.map((val) => (val !== null ? Number(val) : null));

5. Literal Values

The rule flags literal unsafe calls:

Number(null);
Number(undefined);

These are treated as errors, but no auto-fix is offered, because the logic is ambiguous and should be resolved manually.