Skip to content

NaN incorrectly narrows to 0 #36964

Open
Open
@osyrisrblx

Description

@osyrisrblx

TypeScript Version: 3.9.0-dev.20200222

Search Terms: NaN Truthiness Falsy

Code

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 (which is neither boolean, nor 0)

Expected behavior:
Type of x should be number | boolean.

Since NaN isn't a "unit type" like 0, we can't express NaN without using number.

Actual behavior:
Type of x is 0 | boolean.

Playground Link: playground link

Related Issues:
#32778 (Incorrectly marked as duplicate?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions