Skip to content

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

@jeremybparagon

Description

@jeremybparagon

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions