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
This PR improves our support for arrays and tuples in homomorphic mapped types (i.e. structure preserving mapped types of the form { [P in keyof T]: X }). When a homomorphic mapped type is applied to an array or tuple type, we now produce a corresponding array or tuple type where the element type(s) have been transformed.
Previously, we would treat array and tuple types like regular object types and transform all properties (including methods) of the arrays and tuples. This behavior is rarely if ever desired.
Given a homomorphic mapped type { [P in keyof T]: X }, where T is some type variable, the mapping operation depends on T as follows (the first two rules are existing behavior and the remaining are introduced by this PR):
If T is a primitive type no mapping is performed and the result is simply T.
If T is a union type we distribute the mapped type over the union.
If T is an array type S[] we map to an array type R[], where R is an instantiation of X with S substituted for T[P].
If T is an array type ReadonlyArray<S> we map to an array type ReadonlyArray<R>, where R is an instantiation of X with S substituted for T[P].
If T is a tuple [S0, S1, ..., Sn] we map to a tuple type [R0, R1, ..., Rn], where each Rx is an instantiation of X with the corresponding Sx substituted for T[P].
Homomorphic mapped types can use ?, -?, or +? annotations to modify the optional-ness of tuple element types. For example, the predefined Partial<T> and Required<T> types have the expected effects on tuple element types:
This PR improves our support for arrays and tuples in homomorphic mapped types (i.e. structure preserving mapped types of the form
{ [P in keyof T]: X }). When a homomorphic mapped type is applied to an array or tuple type, we now produce a corresponding array or tuple type where the element type(s) have been transformed.Previously, we would treat array and tuple types like regular object types and transform all properties (including methods) of the arrays and tuples. This behavior is rarely if ever desired.
Given a homomorphic mapped type
{ [P in keyof T]: X }, whereTis some type variable, the mapping operation depends onTas follows (the first two rules are existing behavior and the remaining are introduced by this PR):S[]we map to an array typeR[], whereRis an instantiation ofXwithSsubstituted forT[P].ReadonlyArray<S>we map to an array typeReadonlyArray<R>, whereRis an instantiation ofXwithSsubstituted forT[P].[S0, S1, ..., Sn]we map to a tuple type[R0, R1, ..., Rn], where eachRxis an instantiation ofXwith the correspondingSxsubstituted forT[P].Homomorphic mapped types can use
?,-?, or+?annotations to modify the optional-ness of tuple element types. For example, the predefinedPartial<T>andRequired<T>types have the expected effects on tuple element types:In
--strictNullChecksmode the?,-?, or+?annotations also add or removeundefinedfrom the element type(s) of arrays and tuples: