Skip to content
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

T extends (number | null) does not correctly narrow on null checks #35667

Closed
ghost opened this issue Dec 13, 2019 · 3 comments
Closed

T extends (number | null) does not correctly narrow on null checks #35667

ghost opened this issue Dec 13, 2019 · 3 comments

Comments

@ghost
Copy link

ghost commented Dec 13, 2019

TypeScript Version: v3.8.0-dev.20191212

Search Terms: extends null, type guards, inference

Expected behavior: I would expect that both test1 and test2 would behave identically and both type check.

Actual behavior: test2 does not correctly infer that value on the second half of the branch is number, and instead states that Number.isSafeInteger(number | null) is not possible, despite the fact that null was already checked for.

Related Issues: #29627 (closed)

Code

function test1(value: number | null): boolean {
    return value !== null && Number.isSafeInteger(value);
}

function test2<T extends number | null>(value: T): boolean {
    return value !== null && Number.isSafeInteger(value);
}
Output
"use strict";
function test1(value) {
    return value !== null && Number.isSafeInteger(value);
}
function test2(value) {
    return value !== null && Number.isSafeInteger(value);
}
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

@jcalz
Copy link
Contributor

jcalz commented Dec 13, 2019

Duplicate of #13995

@ghost
Copy link
Author

ghost commented Dec 13, 2019

Thank you for redirecting me to the existing bug! I tried to search for it but didn't seem to find it.

Will close this in favour of that one.

@ghost ghost closed this as completed Dec 13, 2019
@RyanCavanaugh RyanCavanaugh reopened this Dec 18, 2019
@RyanCavanaugh
Copy link
Member

Closing again since this was somehow showing up in our "open issues" query

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants