-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Narrowing the type inside forEach doesn't follow the if check from the outside #56854
Comments
Another one for the #9998 pile. |
Yep, and thereβs even a whole issue template explaining not to post this exact issue and everything (βtypes not correct with or in callbackβ)β¦ |
I was looking into FAQ but I didn't find an issue like this. Where does it say to not post those types of issues? |
Itβs like the sixth button when you open a new issueβ¦ but if I were filing a bug report and didnβt already know about it I doubt Iβd notice it. Ideally such a thing would be mentioned inside the bug report template itself, but π€·ββοΈ. |
I would put it into the FAQ since the issue says to check the FAQ first before creating an issue. |
The explanation in question: Iβm honestly surprised it isnβt listed in the βBugs That Arenβt Bugsβ section of the FAQ, thereβs an issue like this posted at least once a week. People hit this all the time. Also relevant is #11498 which discusses a way this might be fixed for certain cases (like this one). |
The reason why I and probably everyone else didn't see the question is because people read from top to bottom until they found a proper issue type. They don't read the rest of the issues just because they may have important information, who does that? I would definitely add this to the FAQ instead of adding a fake issue. I would also get rid of those fake issues that answer the problem and put everything into FAQ. That is supposed to Be a place to read common bugs that are not bugs. |
I just read the fake issue template and I don't really understand. How: Object.entries(style).forEach(([prop, value]) => {
}); is the same as setTimeout which can get executed after delay and something else can modify the code. This code doesn't have any side effects. Those are built-in methods of JavaScript. the function is obviously a callback like in the issue, but shouldn't this work differently than async functions? |
The type system cannot express that a callback is going to be called immediately, so it canβt tell the difference between those two examples. Please see #11498 |
I don't think you're expected to read all the templates (indeed, who does that?), just to recognize that "types not correct in/with callback1" covers your issue and then, when you click it, it gives you more context. Interestingly, people do pick that template sometimes... but for completely unrelated issues that then get erroneously tagged as duplicates π I agree this should be in the FAQ though and I'm surprised it isn't. Footnotes
|
If there was an entry like that in the FAQ I would never create an issue. For me, the callback is always a function passed as an argument to another function. |
π€ |
"First match wins" overload resolution remains a somewhat problematic design aspect of both TypeScript and TypeScript users, apparently π |
That also explains why I only think there's a "Website" button when I talk about that page in the abstract. |
π Search Terms
type narrowing
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ssl=17&ssc=31&pln=17&pc=38#code/MYewdgzgLgBApgWwA5QJ4H0BOBXANnGAXhgAoIALOOKALhgGEBlZtfRy6gGhmFwEMIEdGD4I4daJgCWYAOYBKIgD4YAbwCwAKBg6e4aDBkATOAA8iMClSgA6YIIBKeOBBv45UcgG4tuyx1sZCDhMKCd8EgADGwASVV4BIRExAF81GBTI7mMzeR9tXUxqbEwwf2s7R2cIAG0c0wBdfJT8rVBIWGCodHsICzIAuiYWVDYA7gTBYVFxSyhpOW5oUdnGVjhFQhUNAp12g2D8YCgQTAtouMmkmcz8v3xYHHwLGps3q2pKiHCXBpsAM2MJCeBC2al8fkKxVKMBBhg6fDAwDgIH+DGYaxWPxgADIcbDnDZDnBjqcACpmWCEamWOBHE6YO66FJ5CE6KRokgAQhBih2kMKzgsiBQGBBA2sE34U2SGyZOhSbMMnLhQSgiORqPRI3wPz5Sr8+1g5mIIK5RPW8shAHkAEYAKxJtjgYHmUhcZHW8gBpwAonxgOQSCQakhMCAkNwAG58XDYOANTbbA0Cgn4C0rInUAAK4aQITQJDDEejsfjrN2kJZVr8dsdxxsLrdHuW+G9-z9AaDIeLkZgMbjCaT4MrqZgpizUFzEYLqCLedLg4rY+rSsVmhaQA
π» Code
π Actual behavior
You can't use a variable that was type narrowed inside forEach. You got an error:
π Expected behavior
I expect
rule.style
to work the same inside map.Additional information about the issue
It looks like inside forEach all typechecks are ignored. Another issue is that the
rule
can be undefined insideforEach
but outside it's ok.The text was updated successfully, but these errors were encountered: