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
I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.
Comment
Let's consider this Prettify type:
typePrettify<Textendsobject>=TextendsFunction
? T
: {[PinkeyofT]: T[P]};
There are two key things to note:
T has a constraint of object.
The mapped type { [P in keyof T]: T[P] } is homomorphic, so it should preserve the array structure in case of instantiations with arrays. So, for example, Prettify<[string, string]> returns [string, string].
Now, let's consider this instantiation of Prettify:
// Here, `T` is not constrained with `object`, so we take intersection with `object`typeTest<T>=Prettify<T&object>;typeT1=Test<[string,string]>;// ^? type T1 = { [x: number]: string; 0: string; 1: string; length: 2; toString: () => string; toLocaleSt…
I'd expect T1 to be [string, string] but instead the array structure is lost.
Looks like this happens because of the object intersection:
The example above might be a bit contrived, but this is a common use case—we have object constraints at certain places and to satisfy those constraints we often have to take intersections with object.
I believe this is strongly related to #55386 (comment) . As we can see in #57801 intersections like this can (today) only produce arrays/tuples if they contain only array/tuple types. Mixing with other kinds of types doesn't work.
Acknowledgement
Comment
Let's consider this
Prettify
type:There are two key things to note:
T
has a constraint ofobject
.{ [P in keyof T]: T[P] }
is homomorphic, so it should preserve the array structure in case of instantiations with arrays. So, for example,Prettify<[string, string]>
returns[string, string]
.Now, let's consider this instantiation of
Prettify
:I'd expect
T1
to be[string, string]
but instead the array structure is lost.Looks like this happens because of the
object
intersection:The example above might be a bit contrived, but this is a common use case—we have
object
constraints at certain places and to satisfy those constraints we often have to take intersections withobject
.Playground
The text was updated successfully, but these errors were encountered: