Skip to content

Type guard of Number.isNaN is wrong #12302

Closed
@saschanaz

Description

@saschanaz

TypeScript Version: 2.1 RC bundled in VS2017 RC

Code

let num = 8;
if (!Number.isNaN(num)) { // NumberConstructor.isNaN(value: any): value is number
    printNumber(num + 1); // `num` should still be a number but TS thinks it has `never` type
}

function printNumber(num: number) {
    console.log(`Got ${num} - 1`);
}

Expected behavior: No error

Actual behavior: TS2356 An arithmetic operand must be of type 'any', 'number' or an enum type.

Activity

akarzazi

akarzazi commented on Nov 16, 2016

@akarzazi

This code would raise an other error on num++ since it was declared with const.

Javascript :

const num = 0;
num++;

VM173:2 Uncaught TypeError: Assignment to constant variable

added
BugA bug in TypeScript
Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScript
on Nov 17, 2016
added this to the TypeScript 2.1.3 milestone on Nov 17, 2016
RyanCavanaugh

RyanCavanaugh commented on Nov 17, 2016

@RyanCavanaugh
Member

Introduced via #11670. Seemed like a good idea at the time!

locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @saschanaz@RyanCavanaugh@akarzazi

      Issue actions

        Type guard of Number.isNaN is wrong · Issue #12302 · microsoft/TypeScript