Skip to content

Optional chaining leaks undefined type when using function to narrow type #35520

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
gustavohenke opened this issue Dec 5, 2019 · 2 comments
Closed

Comments

@gustavohenke
Copy link

TypeScript Version: 3.7.2

Search Terms:
Optional chaining

Expected behavior:
Everything compiles successfully.

Actual behavior:
For the x variable:

Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
  Type 'undefined' is not assignable to type 'number'.(2345)

Related Issues:

Code

function checkExists(x: null | undefined, msg?: string, ...args: any[]): never;
function checkExists<T>(x: T | null | undefined): T;
function checkExists<T>(x: T | null | undefined): T {
    if (x == null) {
        throw new Error('argument is null');
    }
    return x;
}

type SomeType = {
    prop: number | undefined;
}

function foo(): SomeType | undefined {
    return { prop: 123 };
}

function bar(val: number) {}

// Doesn't work
const val = foo();
const x = checkExists(val?.prop);
bar(x);

// Works
const y = checkExists(val && val.prop);
bar(y);

// Works
const z = checkExists(val?.prop as number | undefined);
bar(z);
Output
"use strict";
var _a, _b;
function checkExists(x) {
    if (x == null) {
        throw new Error('argument is null');
    }
    return x;
}
function foo() {
    return { prop: 123 };
}
function bar(val) { }
const val = foo();
const x = checkExists((_a = val) === null || _a === void 0 ? void 0 : _a.prop);
bar(x);
const y = checkExists(val && val.prop);
bar(y);
const z = checkExists((_b = val) === null || _b === void 0 ? void 0 : _b.prop);
bar(z);
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

@ajafff
Copy link
Contributor

ajafff commented Dec 5, 2019

Please try with typescript@next as this issue is already fixed in master

@gustavohenke
Copy link
Author

Thanks. Will keep an eye on stable releases.

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