-
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
Inconsistent type widening with results of higher-order functions #51551
Comments
The widening rules are in place to heuristically attempt to infer "better" types and different code is allowed to behave differently under these heuristics Re increment, see #14745 (comment)
When specifying expected behavior, "the same" isn't really actionable. "These two slightly different things should behave identically" quickly runs into transitivity problems -- A1 is close to A2 is close to A3 is close to A4 so each step should be "the same", but if A1 and A4 have manifestly different desirable behaviors, then something has to give somewhere. |
Understood, that type widening is a heuristic that tries to balance psychological expectancy with precise semantics, and that there will be unboundedly many special cases you could consider. For this issue, I'd expect type widening to apply in the same way because locally, all precisely-determined types on right-hand-sides in the |
This sounds like equational reasoning (e.g. when |
This issue has been marked as 'Not a Defect' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
When assigning the value 0 or 1 to the field of an object, see below, type widening infers the type
number
. When doing the same through a generic higher-order function, the field gets the type0 | 1
. This can trigger a type error when using that value in another generic high-order function that assigns to the object as a side-effect.🔎 Search Terms
type widening
inconsistency
🕗 Version & Regression Information
Appears in all versions on Playground (back to 3.3.3333).
Also appears in Nightly (v. 5.0.0.-dev20221116)
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
I get a type error in the function
doesntwork
on themapVal
call. This is because type widening did not apply as expected too
, so it has type{ val: 0 | 1 }
. Simultaneously, type widening is also disabled at the mutationmapVal
-- simply writingo.val += 1
instead of themapVal
call would have worked.🙂 Expected behavior
I expected that the behaviour of type widening remains the same in the case of using high-order callbacks to generate the value and not.
The text was updated successfully, but these errors were encountered: