No contextual typing from return types for boolean literals (#48380)

* No contextual typing from return types for boolean literals

* Accept new baselines

* Add regression tests
This commit is contained in:
Anders Hejlsberg
2022-03-30 16:32:24 -07:00
committed by GitHub
parent e62f960648
commit 3f483d87b4
7 changed files with 230 additions and 11 deletions

View File

@@ -26902,9 +26902,14 @@ namespace ts {
return instantiateInstantiableTypes(contextualType, inferenceContext.nonFixingMapper);
}
// For other purposes (e.g. determining whether to produce literal types) we only
// incorporate inferences made from the return type in a function call.
// incorporate inferences made from the return type in a function call. We remove
// the 'boolean' type from the contextual type such that contextually typed boolean
// literals actually end up widening to 'boolean' (see #48363).
if (inferenceContext.returnMapper) {
return instantiateInstantiableTypes(contextualType, inferenceContext.returnMapper);
const type = instantiateInstantiableTypes(contextualType, inferenceContext.returnMapper);
return type.flags & TypeFlags.Union && containsType((type as UnionType).types, regularFalseType) && containsType((type as UnionType).types, regularTrueType) ?
filterType(type, t => t !== regularFalseType && t !== regularTrueType) :
type;
}
}
}