Skip to content

Commit bff843a

Browse files
committed
Improve error elaboration for invariant generic types
1 parent bf75a3f commit bff843a

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9415,7 +9415,16 @@ namespace ts {
94159415
// with a structural comparison. Otherwise, we know for certain the instantiations aren't
94169416
// related and we can return here.
94179417
if (variances !== emptyArray && !hasCovariantVoidArgument(<TypeReference>target, variances)) {
9418-
return Ternary.False;
9418+
// In some cases generic types that are covariant in regular type checking mode become
9419+
// invariant in --strictFunctionTypes mode because one or more type parameters are used in
9420+
// both co- and contravariant positions. In order to make it easier to diagnose *why* such
9421+
// types are invariant, if any of the type parameters are invariant we reset the reported
9422+
// errors and instead force a structural comparison (which will include elaborations that
9423+
// reveal the reason).
9424+
if (!(reportErrors && some(variances, v => v === Variance.Invariant))) {
9425+
return Ternary.False;
9426+
}
9427+
errorInfo = saveErrorInfo;
94199428
}
94209429
}
94219430
// Even if relationship doesn't hold for unions, intersections, or generic type references,

0 commit comments

Comments
 (0)