Skip to content
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

Control flow analysis does not take effect of closure of inner functions into account #60554

Open
jpilgrim opened this issue Nov 21, 2024 · 2 comments

Comments

@jpilgrim
Copy link

jpilgrim commented Nov 21, 2024

🔎 Search Terms

literal types, arrow function definition, control flow closure

🕗 Version & Regression Information

  • This changed between versions ______ and _______
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about the terms in the title
  • I was unable to test this on prior versions because _______

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.0-dev.20241121#code/GYVwdgxgLglg9mABMAFFAXIgzlATjMAcwEpEBvAKEWsQBsBTKbTAIgEMXEAfRFgI04BeXhwDcVGhAQ5sYemwDWiYSlKCAfOQk0aWZbwHidAX22IYwRGmWDh-FqUo6dWOYtVGapnRat7bdgKOZjq4jCC4SHgg9J7U3jRhUBFIwGy0WLEUxkA

💻 Code

function f(t: string) {
    let s: "a" | "b" = "a";
    const sneak = () => {
        s = "b";
    }
    if (t === "b") {
        sneak();
    }
    if (s === "b") { // <--- here should be no error!
        return true;
    }
    return false;
}

🙁 Actual behavior

In the line marked with a comment, an error is issued:
This comparison appears to be unintentional because the types '"a"' and '"b"' have no overlap.

🙂 Expected behavior

No error should occur here.

Additional information about the issue

In the example, the control flow analysis apparently does not recognise the effect of the inner function definition or its behaviour when computing the actual type of the variable s. This also happens in case of union types, for example. And it also happens for nested function declarations.

@jpilgrim jpilgrim changed the title Control flow analysis does not take effect of arrow function definition into account Control flow analysis does not take effect of closure of inner function definition into account Nov 21, 2024
@jpilgrim jpilgrim changed the title Control flow analysis does not take effect of closure of inner function definition into account Control flow analysis does not take effect of closure of inner functions into account Nov 21, 2024
@Andarist
Copy link
Contributor

#9998

@jpilgrim
Copy link
Author

@Andarist Thanks for the link! I did search for bugs, not for discussions...

Well, the discussion is still open. And I run into that problem again and again. I would consider it a bug (or simply too optimistic).

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

2 participants