Skip to content

Incorrect never type inference. #44025

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
h-joo opened this issue May 10, 2021 · 7 comments
Closed

Incorrect never type inference. #44025

h-joo opened this issue May 10, 2021 · 7 comments
Labels
Duplicate An existing issue was already created

Comments

@h-joo
Copy link
Contributor

h-joo commented May 10, 2021

Bug Report

The type inference does not consider(or be conservative on) side effects of function calls.
Might be a related issue the first failure report in #43819

🔎 Search Terms

does not exist on type never

🕗 Version & Regression Information

  • This changed between versions 4.2.3 and 4.3
  • Also confirmed with the release candidate 4.3.1-rc with git hash d805ce877ae6235565ee697c3c0ce8a95a9c58f6

⏯ Playground Link

Playground link

💻 Code

You need to enable the --strictNullChecks to trigger the bug
./bin/tsc test.ts --strictNullChecks

// test.ts
class A {
    constructor (readonly attrib : number) {}
}
function foo2(callback : any) {
    callback();
}

function foo() {
    let abc :A | null = null;
    
    foo2( () => {
        if( new Date().getTime() > 1000000) {
            abc = new A(123);
        }
    });
    return abc ? abc.attrib : null;
              // compiler error triggers on the abc.attrib
}

🙁 Actual behavior

tsc prints the following error :

test.ts:21:23 - error TS2339: Property 'attrib' does not exist on type 'never'.
21     return abc ? abc.attrib : null;
Found 1 error.

🙂 Expected behavior

Not report the error.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 10, 2021
@RyanCavanaugh
Copy link
Member

Duplicate #11498, #9998. I'm not sure why this ever worked; adding ! in that position clearly shouldn't do anything since it's already known to be not-falsy due to its position in the ? :

@ajafff
Copy link
Contributor

ajafff commented May 10, 2021

Previously a non-null assertion on a variable with inferred never fell back to the declared type of that variable. This bug has been fixed recently.

@h-joo
Copy link
Contributor Author

h-joo commented May 10, 2021

Duplicate #11498, #9998. I'm not sure why this ever worked; adding ! in that position clearly shouldn't do anything since it's already known to be not-falsy due to its position in the ? :

You have a point. It seems a bit weird as you say.

@h-joo
Copy link
Contributor Author

h-joo commented May 10, 2021

I've updated the example from

    return abc ? abc!.attrib : null;

to

    return abc ? abc.attrib : null;

@h-joo
Copy link
Contributor Author

h-joo commented May 10, 2021

Previously a non-null assertion on a variable with inferred never fell back to the declared type of that variable. This bug has been fixed recently.

Sorry for the confusion in the original example, maybe this was not related to the non-null assertion (!.), as it still triggers after I removed the exclamation mark?

@nmain
Copy link

nmain commented May 10, 2021

I've updated the example from

To clarify, with the ! weirdness removed this is still a combination of #9998, #11498, and #8513.

It's also no longer a regression: the oldest version on the playground, TS 3.3.3, behaves the same.

@h-joo
Copy link
Contributor Author

h-joo commented May 10, 2021

I've updated the example from

To clarify, with the ! weirdness removed this is still a combination of #9998, #11498, and #8513.

It's also no longer a regression: the oldest version on the playground, TS 3.3.3, behaves the same.

Great, thank you for the clarifying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants