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
The text was updated successfully, but these errors were encountered:
AnyhowStep
changed the title
Mapped tuple type should treat keys as numeric
Mapped tuple type should treat keys as numeric, and extract types of elements correctly
Feb 3, 2021
Bug Report
π Search Terms
mapped tuple type, key, numeric
π Version & Regression Information
This behaviour has been around for a very long time, I just never bothered to report it.
β― Playground Link
Playground with bug
Playground with workaround
π» Code
π Actual behavior
ArrT[k]
assignable toE
If
k
is a numeric key ofArrT
, thenArrT[k]
is assignable toE
π Expected behavior
ArrT[k]
not assignable toE
Workaround
TakesEHack<>
disables type checking at the call site but it's the most generic workaround that has worked for all my personal use cases.Non-Workarounds
Stuff like the following didn't quite work for all my use cases,
k in keyof ArrT & number
Does not give a mapped tuple type, does not extract each element's type properly
k in Extract<keyof ArrT, number>
Does not give a mapped tuple type, does not extract each element's type properly
TakesE<Extract<ArrT[k], E>>
This triggered complex union errors/infinitely deep type errors (because
TakesE<E>
is too large in my case)I suspect
Extract<>
doesn't work for my case because it makes TS explore one branch per element ofE
.So,
TakesE<Extract<ArrT[k], E>>
becomes a super large union type.However, in my real use cases,
ArrT[k]
is always exactly one element ofE
, meaning I never have large unions in practice.Related issues
#28093
#27995
The text was updated successfully, but these errors were encountered: