Always use strict rules for contravariant inferences

This commit is contained in:
Anders Hejlsberg
2018-01-31 07:48:15 -08:00
parent 7c241ba2f7
commit 5702f61562
2 changed files with 5 additions and 3 deletions

View File

@@ -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;

View File

@@ -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 {