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
{{ message }}
Repository navigation
Mappable tuples with constraints are unwieldy due to lack of covariance #27351
I'm trying to make use of the new "mappable tuple and array types" coming in 3.1, and discovered that it's a bit awkward to work with arrays constrained to a particular type.
The issue here is that even though a homomorphic mapped type only maps the element type(s) of the array or tuple, keyof A still includes the non-numeric properties of Array<T> (i.e. "length", "concat", etc.) and therefore A[keyof A] (the constraint of A[K]) is a large and fairly useless union type that can't be indexed with "foo".
We considered having keyof T for an array or tuple type not include non-numeric properties, but it would be a significant breaking change. For example, A["length"] wouldn't work anymore.
Given where we're at, your conditional type workaround seems reasonable.
When mapping over the keys K in a type T constrained to an array type, is it possible to narrow K instead of T or keyof T? Assuming there were a "numericString" type, the suggestion is to treat
That would possibly give the expected behavior here without pretending that keyof T is limited to the numeric keys, so T['length'] would still exist. Just a passing thought...
Activity
RyanCavanaugh commentedon Oct 1, 2018
Anders Hejlsberg (@ahejlsberg) bug? feature request?
ahejlsberg commentedon Oct 9, 2018
The issue here is that even though a homomorphic mapped type only maps the element type(s) of the array or tuple,
keyof Astill includes the non-numeric properties ofArray<T>(i.e."length","concat", etc.) and thereforeA[keyof A](the constraint ofA[K]) is a large and fairly useless union type that can't be indexed with"foo".We considered having
keyof Tfor an array or tuple type not include non-numeric properties, but it would be a significant breaking change. For example,A["length"]wouldn't work anymore.Given where we're at, your conditional type workaround seems reasonable.
jcalz commentedon Jun 3, 2019
When mapping over the keys
Kin a typeTconstrained to an array type, is it possible to narrowKinstead ofTorkeyof T? Assuming there were a "numericString" type, the suggestion is to treatlike
That would possibly give the expected behavior here without pretending that
keyof Tis limited to the numeric keys, soT['length']would still exist. Just a passing thought...