Skip to content

Narrowing interacts badly with mutation of properties of shared object #61125

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
mfplunet opened this issue Feb 5, 2025 · 4 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@mfplunet
Copy link

mfplunet commented Feb 5, 2025

🔎 Search Terms

"narrowing", "mutation", "shared", "control flow analysis", "have no overlap", "has no overlap"

🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about narrowing.

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.8.0-dev.20250205#code/MYewdgzgLgBATgVzDAvDAFAMwFwYJSoB8MAbiAJYAmBKxm6eA3AFDMA2AprAIa4DeMXjACMMAD4wATDAC+qGAKGiZLbgDpu8yS0Rh0DIguYwTgjfOEsZTZuUwZ1mlGmEE+x06EghOatiABzdAByAAsONn9gmxkgA

💻 Code

const run = (f: () => void) => f(); // This indirection is necessary to reproduce.

let a: { a: 1 | 2 } = { a: 1 };
a.a = 2;
run(() => {
    a.a = 1;
});
// This must be directly in this code flow. For example, two `setTimeout`s where the `if` runs after also does not reproduce the bug.
if (a.a == 1) { // Error: This comparison appears to be unintentional because the types '2' and '1' have no overlap.
    console.log('hello');
}

🙁 Actual behavior

On the if (a.a == 1) { line we see the error This comparison appears to be unintentional because the types '2' and '1' have no overlap..

🙂 Expected behavior

Typescript recognises that the value has been captured into another context that could change the value within the old, not-narrowed type, and consequently undoes the narrowing, i.e., between the run call and the if statement in the example above, a.a would have the type 1 | 2 again.

Additional information about the issue

No response

@nmain
Copy link

nmain commented Feb 5, 2025

Duplicate of #9998. Also see #11498.

@mfplunet
Copy link
Author

mfplunet commented Feb 5, 2025

Yes, it is related, but I think this is more actionable, just don't do the narrowing if the variable is captured into another context.

@jcalz
Copy link
Contributor

jcalz commented Feb 5, 2025

"Just don't narrow closed-over variables" is pessimistic and would result in the bad behavior outlined in #9998, and break a lot of real world code.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 5, 2025
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 8, 2025
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

5 participants