You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Actually, I think #25937 maybe fixes (some of) this. Although undoubtedly Ryan Cavanaugh (@RyanCavanaugh) is probably right - most of the remarks here are caused by the return type widening.
Seems like my proposal at #26979 could be a fix for this. While I don't propose the exact mechanism for literal type inference, I suggest an expression for type assertion that would prevent the type from widening. For example Zhongyang Wang (@k8w)'s code could be written this way:
Would definitely like to see this done. We've hit this over in Pulumi as part of #11312. It seems really unfortunate that something as simple as: new Map(arr.map(a => [a.foo, a.bar])) can't work properly.
I had a PR up that made return widening contextual, rather than always - didn't really get a great chance to review and iterate it before it got out of sync though. #20976 for reference.
Jeremy Chone (@jeremychone) nothing changed w.r.t where we infer tuple types (excepting const contexts) - you'd need to have something with a tuple type on the LHS of that assignment for it to be interpreted as a tuple.
Wesley Wigham (@weswigham) Thank you. In fact, just realized I did not understand the root of the problem (which was that without typing, returning an array could not be assumed to be a tuple).
Activity
HerringtonDarkholme commentedon Nov 2, 2016
Does this effectively mean curried function will be inferred?
[-]Use return type as an inference loacation[/-][+]Use return type as an inference location[/+]hmaurer commentedon Dec 14, 2016
That would be amazing to see. I stumbled upon a similar issue tonight, which is roughly the same as #1212. What is the progress on this?
k8w commentedon Nov 2, 2017
Still have this problem, and also see this:
Both
bandchas compile error:[1,2] is treated as number[] rather than [number, number]
Pajn commentedon Jan 22, 2018
I'm moving the example from #21275 to here as I filed a duplicate
Can I do anything to help get this feature going?
DanielRosenwasser commentedon Mar 26, 2018
Keywords: map on array of tuple contextual contextually typed return type of lambda arrow function expressions
RyanCavanaugh commentedon Aug 1, 2018
I think the only reason this doesn't work is because we widen the return type of the function expression?
weswigham commentedon Aug 1, 2018
Actually, I think #25937 maybe fixes (some of) this. Although undoubtedly Ryan Cavanaugh (@RyanCavanaugh) is probably right - most of the remarks here are caused by the return type widening.
ahejlsberg commentedon Aug 23, 2018
Another example, simplified from #26621:
Would be nice if we could do better here.
cshaa commentedon Sep 8, 2018
Seems like my proposal at #26979 could be a fix for this. While I don't propose the exact mechanism for literal type inference, I suggest an expression for type assertion that would prevent the type from widening. For example Zhongyang Wang (@k8w)'s code could be written this way:
While I agree that implementing the interference mechanism is important, this could provide a quick fix.
CyrusNajmabadi commentedon Oct 27, 2018
Would definitely like to see this done. We've hit this over in Pulumi as part of #11312. It seems really unfortunate that something as simple as:
new Map(arr.map(a => [a.foo, a.bar]))can't work properly.Jessidhia commentedon Nov 9, 2018
Looks like I hit this on DefinitelyTyped/DefinitelyTyped#30057 (comment)
weswigham commentedon Nov 9, 2018
I had a PR up that made return widening contextual, rather than always - didn't really get a great chance to review and iterate it before it got out of sync though. #20976 for reference.
ahejlsberg commentedon Mar 22, 2019
Fixed in #29478.
CyrusNajmabadi commentedon Mar 22, 2019
Thanks much Anders Hejlsberg (@ahejlsberg) ! This will be very helpful in many of our complex, highly generic code spots!
jeremychone commentedon Apr 2, 2019
I must be missing something. I installed Typescript 3.4.1, and still get the wrong inferrence.
weswigham commentedon Apr 2, 2019
Jeremy Chone (@jeremychone) nothing changed w.r.t where we infer tuple types (excepting
constcontexts) - you'd need to have something with a tuple type on the LHS of that assignment for it to be interpreted as a tuple.jeremychone commentedon Apr 2, 2019
Wesley Wigham (@weswigham) Thank you. In fact, just realized I did not understand the root of the problem (which was that without typing, returning an array could not be assumed to be a tuple).