Skip to content

Commit 4af3a3b

Browse files
committed
Lower priority for inferences made from partial reverse mapped types
1 parent 6aaeb52 commit 4af3a3b

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14898,11 +14898,8 @@ namespace ts {
1489814898

1489914899
function createReverseMappedType(source: Type, target: MappedType, constraint: IndexType) {
1490014900
// We consider a source type reverse mappable if it has a string index signature or if
14901-
// it has one or more properties and all properties have inferable types.
14902-
const properties = getPropertiesOfType(source);
14903-
const isReverseMappable = getIndexInfoOfType(source, IndexKind.String) ||
14904-
properties.length !== 0 && every(properties, prop => isPartiallyInferableType(getTypeOfSymbol(prop)));
14905-
if (!isReverseMappable) {
14901+
// it has one or more properties and is of a partially inferable type.
14902+
if (!(getIndexInfoOfType(source, IndexKind.String) || getPropertiesOfType(source).length !== 0 && isPartiallyInferableType(source))) {
1490614903
return undefined;
1490714904
}
1490814905
// For arrays and tuples we infer new arrays and tuples where the reverse mapping has been
@@ -15287,7 +15284,11 @@ namespace ts {
1528715284
const inferredType = inferTypeForHomomorphicMappedType(source, target, <IndexType>constraintType);
1528815285
if (inferredType) {
1528915286
const savePriority = priority;
15290-
priority |= InferencePriority.HomomorphicMappedType;
15287+
// We assign a lower priority to inferences made from types containing non-inferrable
15288+
// types because we may only have a partial result (i.e. we may have failed to make
15289+
// reverse inferences for some properties).
15290+
priority |= getObjectFlags(source) & ObjectFlags.NonInferrableType ?
15291+
InferencePriority.PartialHomomorphicMappedType : InferencePriority.HomomorphicMappedType;
1529115292
inferFromTypes(inferredType, inference.typeParameter);
1529215293
priority = savePriority;
1529315294
}

src/compiler/types.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4405,15 +4405,16 @@ namespace ts {
44054405
export type TypeMapper = (t: TypeParameter) => Type;
44064406

44074407
export const enum InferencePriority {
4408-
NakedTypeVariable = 1 << 0, // Naked type variable in union or intersection type
4409-
HomomorphicMappedType = 1 << 1, // Reverse inference for homomorphic mapped type
4410-
MappedTypeConstraint = 1 << 2, // Reverse inference for mapped type
4411-
ReturnType = 1 << 3, // Inference made from return type of generic function
4412-
LiteralKeyof = 1 << 4, // Inference made from a string literal to a keyof T
4413-
NoConstraints = 1 << 5, // Don't infer from constraints of instantiable types
4414-
AlwaysStrict = 1 << 6, // Always use strict rules for contravariant inferences
4415-
4416-
PriorityImpliesCombination = ReturnType | MappedTypeConstraint | LiteralKeyof, // These priorities imply that the resulting type should be a combination of all candidates
4408+
NakedTypeVariable = 1 << 0, // Naked type variable in union or intersection type
4409+
HomomorphicMappedType = 1 << 1, // Reverse inference for homomorphic mapped type
4410+
PartialHomomorphicMappedType = 1 << 2, // Partial reverse inference for homomorphic mapped type
4411+
MappedTypeConstraint = 1 << 3, // Reverse inference for mapped type
4412+
ReturnType = 1 << 4, // Inference made from return type of generic function
4413+
LiteralKeyof = 1 << 5, // Inference made from a string literal to a keyof T
4414+
NoConstraints = 1 << 6, // Don't infer from constraints of instantiable types
4415+
AlwaysStrict = 1 << 7, // Always use strict rules for contravariant inferences
4416+
4417+
PriorityImpliesCombination = ReturnType | MappedTypeConstraint | LiteralKeyof, // These priorities imply that the resulting type should be a combination of all candidates
44174418
}
44184419

44194420
/* @internal */

0 commit comments

Comments
 (0)