Skip to content

Commit 79a73bf

Browse files
committed
Always cache relations involving intersection types
1 parent e88e596 commit 79a73bf

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18207,13 +18207,12 @@ namespace ts {
1820718207
let result = Ternary.False;
1820818208
const saveErrorInfo = captureErrorCalculationState();
1820918209

18210-
// Note that these checks are specifically ordered to produce correct results. In particular,
18211-
// we need to deconstruct unions before intersections (because unions are always at the top),
18212-
// and we need to handle "each" relations before "some" relations for the same kind of type.
18213-
if (source.flags & TypeFlags.UnionOrIntersection || target.flags & TypeFlags.UnionOrIntersection) {
18214-
result = getConstituentCount(source) * getConstituentCount(target) >= 4 ?
18215-
recursiveTypeRelatedTo(source, target, reportErrors, intersectionState | IntersectionState.UnionIntersectionCheck, recursionFlags) :
18216-
structuredTypeRelatedTo(source, target, reportErrors, intersectionState | IntersectionState.UnionIntersectionCheck);
18210+
if ((source.flags & TypeFlags.Union || target.flags & TypeFlags.Union) && getConstituentCount(source) * getConstituentCount(target) < 4) {
18211+
// We skip caching when source or target is a union with no more than three constituents.
18212+
result = structuredTypeRelatedTo(source, target, reportErrors, intersectionState | IntersectionState.UnionIntersectionCheck);
18213+
}
18214+
else if (source.flags & TypeFlags.UnionOrIntersection || target.flags & TypeFlags.UnionOrIntersection) {
18215+
result = recursiveTypeRelatedTo(source, target, reportErrors, intersectionState | IntersectionState.UnionIntersectionCheck, recursionFlags);
1821718216
}
1821818217
if (!result && !(source.flags & TypeFlags.Union) && (source.flags & (TypeFlags.StructuredOrInstantiable) || target.flags & TypeFlags.StructuredOrInstantiable)) {
1821918218
if (result = recursiveTypeRelatedTo(source, target, reportErrors, intersectionState, recursionFlags)) {
@@ -18677,17 +18676,17 @@ namespace ts {
1867718676
const maybeStart = maybeCount;
1867818677
maybeKeys[maybeCount] = id;
1867918678
maybeCount++;
18679+
const saveExpandingFlags = expandingFlags;
1868018680
if (recursionFlags & RecursionFlags.Source) {
1868118681
sourceStack[sourceDepth] = source;
1868218682
sourceDepth++;
18683+
if (!(expandingFlags & ExpandingFlags.Source) && isDeeplyNestedType(source, sourceStack, sourceDepth)) expandingFlags |= ExpandingFlags.Source;
1868318684
}
1868418685
if (recursionFlags & RecursionFlags.Target) {
1868518686
targetStack[targetDepth] = target;
1868618687
targetDepth++;
18688+
if (!(expandingFlags & ExpandingFlags.Target) && isDeeplyNestedType(target, targetStack, targetDepth)) expandingFlags |= ExpandingFlags.Target;
1868718689
}
18688-
const saveExpandingFlags = expandingFlags;
18689-
if (!(expandingFlags & ExpandingFlags.Source) && isDeeplyNestedType(source, sourceStack, sourceDepth)) expandingFlags |= ExpandingFlags.Source;
18690-
if (!(expandingFlags & ExpandingFlags.Target) && isDeeplyNestedType(target, targetStack, targetDepth)) expandingFlags |= ExpandingFlags.Target;
1869118690
let originalHandler: typeof outofbandVarianceMarkerHandler;
1869218691
let propagatingVarianceFlags: RelationComparisonResult = 0;
1869318692
if (outofbandVarianceMarkerHandler) {
@@ -18713,13 +18712,13 @@ namespace ts {
1871318712
if (outofbandVarianceMarkerHandler) {
1871418713
outofbandVarianceMarkerHandler = originalHandler;
1871518714
}
18716-
expandingFlags = saveExpandingFlags;
1871718715
if (recursionFlags & RecursionFlags.Source) {
1871818716
sourceDepth--;
1871918717
}
1872018718
if (recursionFlags & RecursionFlags.Target) {
1872118719
targetDepth--;
1872218720
}
18721+
expandingFlags = saveExpandingFlags;
1872318722
if (result) {
1872418723
if (result === Ternary.True || (sourceDepth === 0 && targetDepth === 0)) {
1872518724
if (result === Ternary.True || result === Ternary.Maybe) {
@@ -23150,7 +23149,7 @@ namespace ts {
2315023149
}
2315123150

2315223151
function getConstituentCount(type: Type) {
23153-
return type.flags & TypeFlags.UnionOrIntersection ? (type as UnionOrIntersectionType).types.length : 1;
23152+
return type.flags & TypeFlags.Union ? (type as UnionType).types.length : 1;
2315423153
}
2315523154

2315623155
function extractTypesOfKind(type: Type, kind: TypeFlags) {

0 commit comments

Comments
 (0)