Skip to content

Declaring as object or undefined has problems in ts4.3Β #44407

Closed
@casperno

Description

@casperno

Bug Report

πŸ”Ž Search Terms

Searched for 'undefined' in issues after 4.3

πŸ•— Version & Regression Information

  • This changed between versions 4.2.3, and 4.3.2

⏯ Playground Link

Note that the error is show for all 4.1, 4.2 and 4.3. But pasting the same code into a project using 4.1.5 does NOT show the error.

https://www.typescriptlang.org/play?ts=4.3.2&ssl=4&ssc=4&pln=17&pc=6#code/GYVwdgxgLglg9mABMOcAUBKRBvAUIgxAGwFMpEAHAJzgCNSBbALhwEMmwQHaSqBfRAB9E4ACYlgMMCVGIAvCLDjJ00QG5c+QqEiwEiAM5kAaqyIgSmPIRuUa9EgwXZ2ARgAMfDTb6abAen9EKhIjKC0CajpGeUVlKRkNCMRAxAgzIkQ4KAALXkQGMhy4WVzWcnSkUThEVjAAT1ypAHNksNNzSwwkmxhgRDQohwYsa1sCVIg4BgoYUipDVnqDRDLyAHIhxnXEGBWxCQTZOtkABRoKXih6xHXWHerQxDBsxBIADz3yfWvL2+kAG68dbJGxTMAGOCkAB0RDgzUG9kY0NYGGSvj4QA

πŸ’» Code

 let problem: {a:number} | undefined;

 function setValue(){
        problem= {a:10};
    }

    // reset 
    // _this causes compiler to belive that 'problem' can only be undefined from now on
    problem = undefined;

    // call other method that can do anything
    setValue();

    if (problem) {
        _// compiler says that 'problem' is undefined and Property 'a' does not exist on type 'never'_
        console.log(problem.a)
    }

πŸ™ Actual behavior

compiler says that 'problem' is undefined and Property 'a' does not exist on type 'never'

πŸ™‚ Expected behavior

Expected compiler to know that 'problem could be both undefined and {a:number}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions