@@ -9432,6 +9432,7 @@ namespace ts {
94329432
94339433 function structuredTypeRelatedTo(source: Type, target: Type, reportErrors: boolean): Ternary {
94349434 let result: Ternary;
9435+ let originalErrorInfo: DiagnosticMessageChain;
94359436 const saveErrorInfo = errorInfo;
94369437 if (target.flags & TypeFlags.TypeParameter) {
94379438 // A source type { [P in keyof T]: X } is related to a target type T if X is related to T[P].
@@ -9511,6 +9512,7 @@ namespace ts {
95119512 // if we have indexed access types with identical index types, see if relationship holds for
95129513 // the two object types.
95139514 if (result = isRelatedTo((<IndexedAccessType>source).objectType, (<IndexedAccessType>target).objectType, reportErrors)) {
9515+ errorInfo = saveErrorInfo;
95149516 return result;
95159517 }
95169518 }
@@ -9542,6 +9544,10 @@ namespace ts {
95429544 if (!(reportErrors && some(variances, v => v === Variance.Invariant))) {
95439545 return Ternary.False;
95449546 }
9547+ // We remember the original error information so we can restore it in case the structural
9548+ // comparison unexpectedly succeeds. This can happen when the structural comparison result
9549+ // is a Ternary.Maybe for example caused by the recursion depth limiter.
9550+ originalErrorInfo = errorInfo;
95459551 errorInfo = saveErrorInfo;
95469552 }
95479553 }
@@ -9580,8 +9586,11 @@ namespace ts {
95809586 }
95819587 }
95829588 if (result) {
9583- errorInfo = saveErrorInfo;
9584- return result;
9589+ if (!originalErrorInfo) {
9590+ errorInfo = saveErrorInfo;
9591+ return result;
9592+ }
9593+ errorInfo = originalErrorInfo;
95859594 }
95869595 }
95879596 }
0 commit comments