Skip to content

Type guard of Number.isNaN is wrong #12302

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
saschanaz opened this issue Nov 16, 2016 · 2 comments
Closed

Type guard of Number.isNaN is wrong #12302

saschanaz opened this issue Nov 16, 2016 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue

Comments

@saschanaz
Copy link
Contributor

saschanaz commented Nov 16, 2016

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.

@akarzazi
Copy link

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

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript labels Nov 17, 2016
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 2.1.3 milestone Nov 17, 2016
@RyanCavanaugh
Copy link
Member

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants