Repository navigation
Parameter type isn't inferred in values of mapped type with as. #46602
Description
Activity
- changed the title
[-]Parameter type isn't inferred in values of mapped type with "as".[/-][+]Parameter type isn't inferred in values of mapped type with `as`.[/+]on Oct 30, 2021 workaround_2is pretty weird 👀- addedNeeds InvestigationThis issue needs a team member to investigate its status.This issue needs a team member to investigate its status.
on Nov 1, 2021 We currently only implement inference to mapped types without
asclauses. In none of the examples do we actually infer anything forEs, rather we simply default to the constraint'foo' | 'bar', which so happens to match the supplied arguments. So, there's an implied request for a new feature here.That said, it does seem like we should contextually type
_efor each of the methods in the call tobug. We can look at fixing that, but without actual support for inference to mapped types withasclauses, it wouldn't help much.That makes sense. As you said, when removing
onbarfrom each argument,bugandworkaround_1calls are rejected because of the constraint defaulting. I seeworkaround_2is still accepted though. (Playground)This wasn't from any real-world code, so I'll have no complaints if it turns out to be too obscure to be worth addressing. 😃
- addedRescheduledThis issue was previously scheduled to an earlier milestoneThis issue was previously scheduled to an earlier milestone
on May 13, 2022
Bug Report
I came across this odd behavior while doing Day 5 of "Type | Treat".
🔎 Search Terms
as mapped function parameter infer
🕗 Version & Regression Information
I tried every Playground version with
assupport.⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
The types of
onfooandonbarare correctly inferred as(e: string) => voidbut the parameter_eis incorrectly typed asanyinstead ofstring.🙂 Expected behavior
I would expect
_eto have typestring, because it's known that the function has type(e: string) => void. I provided two variations that are typed correctly -- one without usingas, and one forcing the type to be conditional onEs. All three should be equivalent. I'm especially surprised thatbugandworkaround_2behave differently since it's (presumably) just forcingEsto be evaluated slightly earlier.