-
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
Probable bug: Contextual inference does not work in "object-like" scenarios #44999
Comments
@RyanCavanaugh I remember you said "I knew it'd be interesting (usually implies time-consuming, but not this time)" so is this a trivial fix? Because if so and if possible I'd love to have this in 4.4 or maybe the next milestone (this directly impacts the xstate types I'm working on at this very moment and the workaround I use is pretty nasty). Thanks! |
Yeah, this is a tricky one, and effectively a design limitation. The issue is captured in this comment in
The comment also explains why your workaround works--adding a call signature to the return type of |
Thanks a lot for your answer @ahejlsberg! Yeah I totally understand TypeScript's problem and even agree that the heuristic it applies is a good one (xstate just happens to have some very corner case scenarios), heck that's what made me come up with the workaround because intuitively I knew what's going on haha. Though if I were to make a critic, I think the foundational problem is TypeScript (to put it naively) doesn't leverage or understand that some functions are essentially "sugars", it can be very easily identified if I write assign as It would be cool if TypeScript perhaps uses some heuristic to identity sugar-like functions, or if I were to propose a solution it would be that TypeScript provides a type that allows the user to mark an inference site to be of lesser priority so that we can type assign like Thanks again for your time! |
@ahejlsberg being able to inform TS, even "manually", with some kind of annotation which path do we want to take here would be super lovely for some libraries (like XState π ). This would be a powerful tool for library authors. I'm slightly unsure how to treat this issue now because it has been classified as a Design Limitation but it has not been closed. Is this something that is still under consideration but perhaps it has a super low priority? I just wonder if you might ever revisit this kind of thing. |
We haven't updated the automation to automatically close these. Anything classed this way is in an indefinite "hope we can figure it out eventually!" state; things are revisited naturally as people continue to encounter them. |
Bug Report
π Search Terms
Contextual inference
π Version & Regression Information
tested with 4.4.0-beta
β― Playground Link
Playground
π» Code
π Actual behavior
assign
infers type parameterC
asunknown
π Expected behavior
assign
should infer type parameterC
as{ foo: number }
Filing as Ryan said here. Fwiw I think a more minimal repro would be this.
The text was updated successfully, but these errors were encountered: