Skip to content

Commit 5702f61

Browse files
committed
Always use strict rules for contravariant inferences
1 parent 7c241ba commit 5702f61

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

src/compiler/checker.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8129,8 +8129,9 @@ namespace ts {
81298129
if (inferTypeParameters) {
81308130
const inferences = map(inferTypeParameters, createInferenceInfo);
81318131
// We don't want inferences from constraints as they may cause us to eagerly resolve the
8132-
// conditional type instead of deferring resolution.
8133-
inferTypes(inferences, checkType, extendsType, InferencePriority.NoConstraints);
8132+
// conditional type instead of deferring resolution. Also, we always want strict function
8133+
// types rules (i.e. proper contravariance) for inferences.
8134+
inferTypes(inferences, checkType, extendsType, InferencePriority.NoConstraints | InferencePriority.AlwaysStrict);
81348135
// We infer 'never' when there are no candidates for a type parameter
81358136
const inferredTypes = map(inferences, inference => getTypeFromInference(inference) || neverType);
81368137
const inferenceMapper = createTypeMapper(inferTypeParameters, inferredTypes);
@@ -11510,7 +11511,7 @@ namespace ts {
1151011511
}
1151111512

1151211513
function inferFromContravariantTypes(source: Type, target: Type) {
11513-
if (strictFunctionTypes) {
11514+
if (strictFunctionTypes || priority & InferencePriority.AlwaysStrict) {
1151411515
contravariant = !contravariant;
1151511516
inferFromTypes(source, target);
1151611517
contravariant = !contravariant;

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3884,6 +3884,7 @@ namespace ts {
38843884
MappedType = 1 << 1, // Reverse inference for mapped type
38853885
ReturnType = 1 << 2, // Inference made from return type of generic function
38863886
NoConstraints = 1 << 3, // Don't infer from constraints of instantiable types
3887+
AlwaysStrict = 1 << 4, // Always use strict rules for contravariant inferences
38873888
}
38883889

38893890
export interface InferenceInfo {

0 commit comments

Comments
 (0)