Skip to content

Strict null checks and flow control of returned functions #9263

@blakeembrey

Description

@blakeembrey

TypeScript Version:

Version 1.9.0-dev.20160619-1.0

Code

function doThing (x?: { prop: boolean }) {
  if (x == null) {
    return function () {
      throw new TypeError('Not correctly set up')
    }
  }

  return function () {
    return x.prop
  }
}
{
  "compilerOptions": {
    "strictNullChecks": true
  }
}

Expected behavior: Successful compilation.

Actual behavior:

index.ts(9,12): error TS2532: Object is possibly 'undefined'.

I honestly wasn't sure what this exact feature would be called to find a duplicate, and I know I've logged a similar issues issues in the past. Let me know if a duplicate does exist. In any case, I would expect flow control to have understood the undefined case was already handled. I suppose this could be related to the fact the arguments are mutable? Is it possible to use flow control analysis to understand nothing re-assigns the value (in fact, nothing else even executes) after that return?

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions