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

Type narrowing for optional chaining in OR expression in AND expression #35109

Open
jeremybparagon opened this issue Nov 14, 2019 · 2 comments
Open
Labels
Bug A bug in TypeScript
Milestone

Comments

@jeremybparagon
Copy link

jeremybparagon commented Nov 14, 2019

TypeScript Nightly version 3.8.0-dev.20191113
Playground link

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,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Input:

const f = (a: { b: boolean, c: number } | undefined) => (a?.b || null) && a.c;

Output:
Compiler error: Object is possibly 'undefined'.(2532)

Expected behavior:
Successful compilation

Note:

const f = (a: { b: boolean, c: number } | undefined) => (a?.b || false) && a.c;

(using false instead of null) works as expected.

Related issues:
#34570
#33806

@fatcerberus
Copy link

Possibly also related to #34974?

@andrewbranch
Copy link
Member

andrewbranch commented Dec 10, 2019

I think the issue with #34974 is fairly specific to the usage of the type predicate. Since this control flow all happens inline, it looks more obviously buggy to me.

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

No branches or pull requests

4 participants