Skip to content

Number.isFinite invalid #50727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lucioreyli opened this issue Sep 12, 2022 · 2 comments
Closed

Number.isFinite invalid #50727

lucioreyli opened this issue Sep 12, 2022 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@lucioreyli
Copy link

TypeScript is not respecting the callback of Number.isFinite.

Example

type Product = {
    quantity?: number
};

const product: undefined | Product = {
    quantity: 34,
}

// TypeScript error! product.quantity > 3 returns error because can be undefined (?? what)
if(Number.isFinite(product?.quantity) && product.quantity > 3) { }

Explanation

Number.isFinite always return false to every variable that is not number (or better, typeof equals number). You can try all the types (including objects) and will fail if the parameter to Number.isFinite is not a number.

Expected return

// No errors is right-hand conditional because the left conditional need be a number to continue👇!!
if(Number.isFinite(product?.quantity) && product.quantity > 3) { } // ✅

Solution (partial)

To resolve this for a while, can replace the

if(Number.isFinite(product?.quantity) && product.quantity > 3) { }

to

if(typeof product?.quantity === 'number' && product.quantity > 3) { }
@lucioreyli lucioreyli added the Duplicate An existing issue was already created label Sep 12, 2022
@lucioreyli lucioreyli changed the title Number.isFinite invalid secondary Number.isFinite invalid Sep 12, 2022
@whzx5byb
Copy link

Duplicate of #39090

@fatcerberus
Copy link

There are no callback functions involved in this example…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants