diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index b81ee0b8864d8..7eefd10afd3ea 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -11789,7 +11789,7 @@ namespace ts { const inferredType = inferTypeForHomomorphicMappedType(source, target); if (inferredType) { const savePriority = priority; - priority |= InferencePriority.MappedType; + priority |= InferencePriority.HomomorphicMappedType; inferFromTypes(inferredType, inference.typeParameter); priority = savePriority; } @@ -11799,7 +11799,10 @@ namespace ts { if (constraintType.flags & TypeFlags.TypeParameter) { // We're inferring from some source type S to a mapped type { [P in T]: X }, where T is a type // parameter. Infer from 'keyof S' to T and infer from a union of each property type in S to X. + const savePriority = priority; + priority |= InferencePriority.MappedTypeConstraint; inferFromTypes(getIndexType(source), constraintType); + priority = savePriority; inferFromTypes(getUnionType(map(getPropertiesOfType(source), getTypeOfSymbol)), getTemplateTypeFromMappedType(target)); return; } @@ -11932,7 +11935,7 @@ namespace ts { // If all inferences were made from contravariant positions, infer a common subtype. Otherwise, if // union types were requested or if all inferences were made from the return type position, infer a // union type. Otherwise, infer a common supertype. - const unwidenedType = context.flags & InferenceFlags.InferUnionTypes || inference.priority & InferencePriority.ReturnType ? + const unwidenedType = context.flags & InferenceFlags.InferUnionTypes || inference.priority & InferencePriority.PriorityImpliesUnion ? getUnionType(baseCandidates, UnionReduction.Subtype) : getCommonSupertype(baseCandidates); inferredType = getWidenedType(unwidenedType); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 5e2bce4d72e87..3db40d43e12bb 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3919,11 +3919,14 @@ namespace ts { export type TypeMapper = (t: TypeParameter) => Type; export const enum InferencePriority { - NakedTypeVariable = 1 << 0, // Naked type variable in union or intersection type - MappedType = 1 << 1, // Reverse inference for mapped type - ReturnType = 1 << 2, // Inference made from return type of generic function - NoConstraints = 1 << 3, // Don't infer from constraints of instantiable types - AlwaysStrict = 1 << 4, // Always use strict rules for contravariant inferences + NakedTypeVariable = 1 << 0, // Naked type variable in union or intersection type + HomomorphicMappedType = 1 << 1, // Reverse inference for homomorphic mapped type + MappedTypeConstraint = 1 << 2, // Reverse inference for mapped type + ReturnType = 1 << 3, // Inference made from return type of generic function + NoConstraints = 1 << 4, // Don't infer from constraints of instantiable types + AlwaysStrict = 1 << 5, // Always use strict rules for contravariant inferences + + PriorityImpliesUnion = ReturnType | MappedTypeConstraint, // These priorities imply that the resulting type should be a union of all candidates } /* @internal */ diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index 90ed78047e6a6..6763e768cda98 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -2236,10 +2236,12 @@ declare namespace ts { } enum InferencePriority { NakedTypeVariable = 1, - MappedType = 2, - ReturnType = 4, - NoConstraints = 8, - AlwaysStrict = 16, + HomomorphicMappedType = 2, + MappedTypeConstraint = 4, + ReturnType = 8, + NoConstraints = 16, + AlwaysStrict = 32, + PriorityImpliesUnion = 12, } interface JsFileExtensionInfo { extension: string; diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index c1f088c058bf9..2f467c9ffb07f 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -2236,10 +2236,12 @@ declare namespace ts { } enum InferencePriority { NakedTypeVariable = 1, - MappedType = 2, - ReturnType = 4, - NoConstraints = 8, - AlwaysStrict = 16, + HomomorphicMappedType = 2, + MappedTypeConstraint = 4, + ReturnType = 8, + NoConstraints = 16, + AlwaysStrict = 32, + PriorityImpliesUnion = 12, } interface JsFileExtensionInfo { extension: string; diff --git a/tests/baselines/reference/mappedTypeMultiInference.js b/tests/baselines/reference/mappedTypeMultiInference.js new file mode 100644 index 0000000000000..1ce8e6b878565 --- /dev/null +++ b/tests/baselines/reference/mappedTypeMultiInference.js @@ -0,0 +1,34 @@ +//// [mappedTypeMultiInference.ts] +interface Style { + flashy: any; +} + +declare function mergeStyleSets( + ...cssSets: { [P in K]?: Style }[]): { [P in K]: Style }; + +// Expected: +// let x: { +// a: Style; +// b: Style; +// } +let x = mergeStyleSets( + {}, + { + a: { flashy: true }, + }, + { + b: { flashy: true }, + }, +) + +//// [mappedTypeMultiInference.js] +// Expected: +// let x: { +// a: Style; +// b: Style; +// } +var x = mergeStyleSets({}, { + a: { flashy: true } +}, { + b: { flashy: true } +}); diff --git a/tests/baselines/reference/mappedTypeMultiInference.symbols b/tests/baselines/reference/mappedTypeMultiInference.symbols new file mode 100644 index 0000000000000..216d0746f4ce6 --- /dev/null +++ b/tests/baselines/reference/mappedTypeMultiInference.symbols @@ -0,0 +1,44 @@ +=== tests/cases/compiler/mappedTypeMultiInference.ts === +interface Style { +>Style : Symbol(Style, Decl(mappedTypeMultiInference.ts, 0, 0)) + + flashy: any; +>flashy : Symbol(Style.flashy, Decl(mappedTypeMultiInference.ts, 0, 17)) +} + +declare function mergeStyleSets( +>mergeStyleSets : Symbol(mergeStyleSets, Decl(mappedTypeMultiInference.ts, 2, 1)) +>K : Symbol(K, Decl(mappedTypeMultiInference.ts, 4, 32)) + + ...cssSets: { [P in K]?: Style }[]): { [P in K]: Style }; +>cssSets : Symbol(cssSets, Decl(mappedTypeMultiInference.ts, 4, 50)) +>P : Symbol(P, Decl(mappedTypeMultiInference.ts, 5, 19)) +>K : Symbol(K, Decl(mappedTypeMultiInference.ts, 4, 32)) +>Style : Symbol(Style, Decl(mappedTypeMultiInference.ts, 0, 0)) +>P : Symbol(P, Decl(mappedTypeMultiInference.ts, 5, 44)) +>K : Symbol(K, Decl(mappedTypeMultiInference.ts, 4, 32)) +>Style : Symbol(Style, Decl(mappedTypeMultiInference.ts, 0, 0)) + +// Expected: +// let x: { +// a: Style; +// b: Style; +// } +let x = mergeStyleSets( +>x : Symbol(x, Decl(mappedTypeMultiInference.ts, 12, 3)) +>mergeStyleSets : Symbol(mergeStyleSets, Decl(mappedTypeMultiInference.ts, 2, 1)) + + {}, + { + a: { flashy: true }, +>a : Symbol(a, Decl(mappedTypeMultiInference.ts, 14, 5)) +>flashy : Symbol(flashy, Decl(mappedTypeMultiInference.ts, 15, 12)) + + }, + { + b: { flashy: true }, +>b : Symbol(b, Decl(mappedTypeMultiInference.ts, 17, 5)) +>flashy : Symbol(flashy, Decl(mappedTypeMultiInference.ts, 18, 12)) + + }, +) diff --git a/tests/baselines/reference/mappedTypeMultiInference.types b/tests/baselines/reference/mappedTypeMultiInference.types new file mode 100644 index 0000000000000..d07a60f3f4bab --- /dev/null +++ b/tests/baselines/reference/mappedTypeMultiInference.types @@ -0,0 +1,54 @@ +=== tests/cases/compiler/mappedTypeMultiInference.ts === +interface Style { +>Style : Style + + flashy: any; +>flashy : any +} + +declare function mergeStyleSets( +>mergeStyleSets : (...cssSets: { [P in K]?: Style; }[]) => { [P in K]: Style; } +>K : K + + ...cssSets: { [P in K]?: Style }[]): { [P in K]: Style }; +>cssSets : { [P in K]?: Style; }[] +>P : P +>K : K +>Style : Style +>P : P +>K : K +>Style : Style + +// Expected: +// let x: { +// a: Style; +// b: Style; +// } +let x = mergeStyleSets( +>x : { a: Style; b: Style; } +>mergeStyleSets( {}, { a: { flashy: true }, }, { b: { flashy: true }, },) : { a: Style; b: Style; } +>mergeStyleSets : (...cssSets: { [P in K]?: Style; }[]) => { [P in K]: Style; } + + {}, +>{} : {} + { +>{ a: { flashy: true }, } : { a: { flashy: boolean; }; } + + a: { flashy: true }, +>a : { flashy: boolean; } +>{ flashy: true } : { flashy: boolean; } +>flashy : boolean +>true : true + + }, + { +>{ b: { flashy: true }, } : { b: { flashy: boolean; }; } + + b: { flashy: true }, +>b : { flashy: boolean; } +>{ flashy: true } : { flashy: boolean; } +>flashy : boolean +>true : true + + }, +) diff --git a/tests/cases/compiler/mappedTypeMultiInference.ts b/tests/cases/compiler/mappedTypeMultiInference.ts new file mode 100644 index 0000000000000..46d2093fc2ad7 --- /dev/null +++ b/tests/cases/compiler/mappedTypeMultiInference.ts @@ -0,0 +1,21 @@ +interface Style { + flashy: any; +} + +declare function mergeStyleSets( + ...cssSets: { [P in K]?: Style }[]): { [P in K]: Style }; + +// Expected: +// let x: { +// a: Style; +// b: Style; +// } +let x = mergeStyleSets( + {}, + { + a: { flashy: true }, + }, + { + b: { flashy: true }, + }, +) \ No newline at end of file