Skip to content

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

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

Closed
casperno opened this issue Jun 3, 2021 · 6 comments
Closed

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

casperno opened this issue Jun 3, 2021 · 6 comments

Comments

@casperno
Copy link

casperno commented Jun 3, 2021

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}

@MartinJohns
Copy link
Contributor

You forgot to fill out the issue template for bug reports.


Works in 4.2.3

Doesn't work in 4.2.3 either: Playground link


Essentially a duplicate of #9998.

@casperno
Copy link
Author

casperno commented Jun 3, 2021

Sorry about the missing template, it's my first report. And thanks for the quick response

I too see that the Playground shows errors for 4.3, 4.2 and even 4.1.

But if I paste this code into a project that is set up with 4.1, it works.
And I've just refactored a lot of code that didn't fail with 4.2, but fails with 4.3.

So there seems to have been a change in 4.3 that takes it beyond #9998

@fatcerberus
Copy link

Your other project may not have strictNullChecks enabled.

@casperno
Copy link
Author

casperno commented Jun 3, 2021

Neither project has strictNullChecks enabled, but strict is true for the 4.2 project.

Setting strict = true for 4.1 project give the same error,
strict = true, strictNullChecks = false removes it.

Looks like this is the cause.

Has default behaviour of strict = true changed from 4.2 to 4.3?

@MartinJohns
Copy link
Contributor

You see this behavior due to a fixed bug. See #44025.

@casperno
Copy link
Author

casperno commented Jun 3, 2021

Thank you both for the swift response and for clearing up my confusion.

Typescript rocks, keep up the excellent work!

@casperno casperno closed this as completed Jun 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants