mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-30 11:24:49 -05:00
Improve reduction of similar intersections in type inference (#51405)
* Change criteria for reducing intersections in type inference * Add regression test
This commit is contained in:
@@ -22728,14 +22728,11 @@ namespace ts {
|
||||
}
|
||||
source = getUnionType(sources);
|
||||
}
|
||||
else if (target.flags & TypeFlags.Intersection && some((target as IntersectionType).types,
|
||||
t => !!getInferenceInfoForType(t) || (isGenericMappedType(t) && !!getInferenceInfoForType(getHomomorphicTypeVariable(t) || neverType)))) {
|
||||
// We reduce intersection types only when they contain naked type parameters. For example, when
|
||||
// inferring from 'string[] & { extra: any }' to 'string[] & T' we want to remove string[] and
|
||||
else if (target.flags & TypeFlags.Intersection && !every((target as IntersectionType).types, isNonGenericObjectType)) {
|
||||
// We reduce intersection types unless they're simple combinations of object types. For example,
|
||||
// when inferring from 'string[] & { extra: any }' to 'string[] & T' we want to remove string[] and
|
||||
// infer { extra: any } for T. But when inferring to 'string[] & Iterable<T>' we want to keep the
|
||||
// string[] on the source side and infer string for T.
|
||||
// Likewise, we consider a homomorphic mapped type constrainted to the target type parameter as similar to a "naked type variable"
|
||||
// in such scenarios.
|
||||
if (!(source.flags & TypeFlags.Union)) {
|
||||
// Infer between identically matching source and target constituents and remove the matching types.
|
||||
const [sources, targets] = inferFromMatchingTypes(source.flags & TypeFlags.Intersection ? (source as IntersectionType).types : [source], (target as IntersectionType).types, isTypeIdenticalTo);
|
||||
|
||||
Reference in New Issue
Block a user