Skip to content

strictNullChecks false positives in case clause #24091

Open
@ajafff

Description

@ajafff

TypeScript Version: 2.9.0-dev.20180512

Search Terms:

Code

// @strictNullChecks: true

function test1(param?: {prop: string}) {
  switch (param && param.prop) {
    case 'foo':
      return param.prop; // error: 'param' is possibly undefined
  }
}

function test2(param: {prop?: string}) {
  switch (param && param.prop) {
    case 'foo':
      return param.prop.charAt(0); // error: 'param.prop' is possibly undefined
  }
}

Expected behavior:

Compiles without error.

Actual behavior:

strictNullChecks errors as described in the comments above.

Playground Link: https://agentcooper.github.io/typescript-play/?noImplicitReturns=false#code/GYVwdgxgLglg9mABFApgZygRgBQAcCGATvgLYD8AXIgN66Fy5UaExgDmAvgJQ0BQiiNAHcYUCAAtEeIqUQAyOYgLESAOjoMe1fgMQR8aFIgDkwOHGMUduxIRRQQhJMtLr6uANyIA9N8QpCekIqYxcSY0QYNCU4NDQYACMAGwBPRHAAExRgVhQMnQ5eQt5QSFgEZHQoACZpFSpad0pBKBZ2bj4BYVEJKTD5RTC3TU7dfUMTMwsrGwE7ByclGTUNXFUJIgBBKGwABi4vX39AuGCTIdWIqJi4xNT0sCycsDyCoqA

Related Issues:
#23818

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