Skip to content

TS doesn't understand that NaN is a falsy number #32778

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
Validark opened this issue Aug 9, 2019 · 3 comments
Closed

TS doesn't understand that NaN is a falsy number #32778

Validark opened this issue Aug 9, 2019 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@Validark
Copy link

Validark commented Aug 9, 2019

TypeScript Version: latest beta

Search Terms: NaN truthiness falsy 0 is not the only falsy number

Code

function f(a: number, b: string) {
    return a && b;
}

const x: string | 0 = f(0 / 0, "Hello"); // should error but doesn't

Expected behavior: function f should return a string or number, since there currently is no NaN type in TS. If there was, it would return 0 | NaN | string

Actual behavior: function f returns string | 0

@RyanCavanaugh
Copy link
Member

Duplicate #15135

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 9, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@osyrisrblx
Copy link

osyrisrblx commented Feb 10, 2020

@RyanCavanaugh I don't think this is actually a duplicate of that issue. The issue you linked is more along the lines of "these are terms in JavaScript and should exist in TypeScript".

Rather, this is a truthiness bug! Here's a more simple example:

function bool(): boolean { return false; }
function number(): number { return NaN; }

let a = number();
let b = bool();

const x = a && b; // type = boolean | 0
console.log(x); // prints NaN

I would expect the type of x here to be boolean | number

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

4 participants