From 5702f61562f3dc2719394c25efd2e737f733be1a Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Wed, 31 Jan 2018 07:48:15 -0800 Subject: [PATCH] Always use strict rules for contravariant inferences --- src/compiler/checker.ts | 7 ++++--- src/compiler/types.ts | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 1584038430f..49885fffb1c 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -8129,8 +8129,9 @@ namespace ts { if (inferTypeParameters) { const inferences = map(inferTypeParameters, createInferenceInfo); // We don't want inferences from constraints as they may cause us to eagerly resolve the - // conditional type instead of deferring resolution. - inferTypes(inferences, checkType, extendsType, InferencePriority.NoConstraints); + // conditional type instead of deferring resolution. Also, we always want strict function + // types rules (i.e. proper contravariance) for inferences. + inferTypes(inferences, checkType, extendsType, InferencePriority.NoConstraints | InferencePriority.AlwaysStrict); // We infer 'never' when there are no candidates for a type parameter const inferredTypes = map(inferences, inference => getTypeFromInference(inference) || neverType); const inferenceMapper = createTypeMapper(inferTypeParameters, inferredTypes); @@ -11510,7 +11511,7 @@ namespace ts { } function inferFromContravariantTypes(source: Type, target: Type) { - if (strictFunctionTypes) { + if (strictFunctionTypes || priority & InferencePriority.AlwaysStrict) { contravariant = !contravariant; inferFromTypes(source, target); contravariant = !contravariant; diff --git a/src/compiler/types.ts b/src/compiler/types.ts index f9b06bc059c..260ac6e744e 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -3884,6 +3884,7 @@ namespace ts { 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 } export interface InferenceInfo {