@@ -844,6 +844,7 @@ namespace ts {
844
844
const symbolLinks: SymbolLinks[] = [];
845
845
const nodeLinks: NodeLinks[] = [];
846
846
const flowLoopCaches: Map<Type>[] = [];
847
+ const flowLoopContainingUnionCache: (UnionType | undefined)[] = [];
847
848
const flowAssignmentTypes: Type[] = [];
848
849
const flowLoopNodes: FlowNode[] = [];
849
850
const flowLoopKeys: string[] = [];
@@ -19004,8 +19005,8 @@ namespace ts {
19004
19005
return key = getFlowCacheKey(reference, declaredType, initialType, flowContainer);
19005
19006
}
19006
19007
19007
- function captureContainingUnion(type: FlowType) {
19008
- if (!isIncomplete(type) && type.flags & TypeFlags.Union) {
19008
+ function captureContainingUnion(type: FlowType, flags?: FlowFlags ) {
19009
+ if ((flags === undefined || (flags & FlowFlags.BranchLabel) === 0) && !isIncomplete(type) && type.flags & TypeFlags.Union) {
19009
19010
containingUnion = type as UnionType;
19010
19011
}
19011
19012
}
@@ -19107,7 +19108,7 @@ namespace ts {
19107
19108
sharedFlowTypes[sharedFlowCount] = type;
19108
19109
sharedFlowCount++;
19109
19110
}
19110
- captureContainingUnion(type);
19111
+ captureContainingUnion(type, flags );
19111
19112
flowDepth--;
19112
19113
return type;
19113
19114
}
@@ -19328,6 +19329,7 @@ namespace ts {
19328
19329
// are the same), there is no reason to process more antecedents since the only
19329
19330
// possible outcome is subtypes that will be removed in the final union type anyway.
19330
19331
if (type === declaredType && declaredType === initialType) {
19332
+ containingUnion = undefined;
19331
19333
return type;
19332
19334
}
19333
19335
pushIfUnique(antecedentTypes, type);
@@ -19342,8 +19344,13 @@ namespace ts {
19342
19344
seenIncomplete = true;
19343
19345
}
19344
19346
}
19345
- const containingUnionType = createFlowType(getUnionOrEvolvingArrayType(containedUnions, subtypeReduction ? UnionReduction.Subtype : UnionReduction.Literal), seenIncomplete);
19346
- captureContainingUnion(containingUnionType);
19347
+ containingUnion = undefined;
19348
+ captureContainingUnion(
19349
+ createFlowType(
19350
+ getUnionOrEvolvingArrayType(containedUnions, subtypeReduction ? UnionReduction.Subtype : UnionReduction.Literal),
19351
+ seenIncomplete
19352
+ )
19353
+ );
19347
19354
return createFlowType(getUnionOrEvolvingArrayType(antecedentTypes, subtypeReduction ? UnionReduction.Subtype : UnionReduction.Literal), seenIncomplete);
19348
19355
}
19349
19356
@@ -19359,6 +19366,7 @@ namespace ts {
19359
19366
}
19360
19367
const cached = cache.get(key);
19361
19368
if (cached) {
19369
+ containingUnion = flowLoopContainingUnionCache[id];
19362
19370
return cached;
19363
19371
}
19364
19372
// If this flow loop junction and reference are already being processed, return
@@ -19379,12 +19387,14 @@ namespace ts {
19379
19387
const antecedentTypes: Type[] = [];
19380
19388
let subtypeReduction = false;
19381
19389
let firstAntecedentType: FlowType | undefined;
19390
+ let unionContainedAtTop: UnionType | undefined;
19382
19391
for (const antecedent of flow.antecedents!) {
19383
19392
let flowType;
19384
19393
if (!firstAntecedentType) {
19385
19394
// The first antecedent of a loop junction is always the non-looping control
19386
19395
// flow path that leads to the top.
19387
19396
flowType = firstAntecedentType = getTypeAtFlowNode(antecedent);
19397
+ unionContainedAtTop = containingUnion;
19388
19398
}
19389
19399
else {
19390
19400
// All but the first antecedent are the looping control flow paths that lead
@@ -19400,6 +19410,7 @@ namespace ts {
19400
19410
// the resulting type and bail out.
19401
19411
const cached = cache.get(key);
19402
19412
if (cached) {
19413
+ containingUnion = flowLoopContainingUnionCache[id];
19403
19414
return cached;
19404
19415
}
19405
19416
}
@@ -19422,8 +19433,10 @@ namespace ts {
19422
19433
// is incomplete.
19423
19434
const result = getUnionOrEvolvingArrayType(antecedentTypes, subtypeReduction ? UnionReduction.Subtype : UnionReduction.Literal);
19424
19435
if (isIncomplete(firstAntecedentType!)) {
19436
+ containingUnion = undefined;
19425
19437
return createFlowType(result, /*incomplete*/ true);
19426
19438
}
19439
+ containingUnion = flowLoopContainingUnionCache[id] = unionContainedAtTop;
19427
19440
cache.set(key, result);
19428
19441
return result;
19429
19442
}
0 commit comments