Correct tuple relations in --strictOptionalProperties mode (#44568)

* Fix relations for optional elements in tuples with --strictOptionalProperties

* Accept new baselines

* Add regression test

* Address CR feedback
This commit is contained in:
Anders Hejlsberg
2021-06-13 12:53:13 -07:00
committed by GitHub
parent 6a1623d413
commit 5540364c12
12 changed files with 103 additions and 31 deletions

View File

@@ -15095,11 +15095,11 @@ namespace ts {
}
function isTypicalNondistributiveConditional(root: ConditionalRoot) {
return !root.isDistributive
&& root.node.checkType.kind === SyntaxKind.TupleType
&& length((root.node.checkType as TupleTypeNode).elements) === 1
&& root.node.extendsType.kind === SyntaxKind.TupleType
&& length((root.node.extendsType as TupleTypeNode).elements) === 1;
return !root.isDistributive && isSingletonTupleType(root.node.checkType) && isSingletonTupleType(root.node.extendsType);
}
function isSingletonTupleType(node: TypeNode) {
return isTupleTypeNode(node) && length(node.elements) === 1 && !isOptionalTypeNode(node.elements[0]) && !isRestTypeNode(node.elements[0]);
}
/**
@@ -18711,7 +18711,7 @@ namespace ts {
return Ternary.False;
}
if (getObjectFlags(source) & ObjectFlags.Reference && getObjectFlags(target) & ObjectFlags.Reference && (source as TypeReference).target === (target as TypeReference).target &&
!(getObjectFlags(source) & ObjectFlags.MarkerType || getObjectFlags(target) & ObjectFlags.MarkerType)) {
!isTupleType(source) && !(getObjectFlags(source) & ObjectFlags.MarkerType || getObjectFlags(target) & ObjectFlags.MarkerType)) {
// We have type references to the same generic type, and the type references are not marker
// type references (which are intended by be compared structurally). Obtain the variance
// information for the type parameters and relate the type arguments accordingly.
@@ -19182,7 +19182,7 @@ namespace ts {
removeMissingType(targetType, !!(targetFlags & ElementFlags.Optional));
const related = isRelatedTo(sourceType, targetCheckType, reportErrors, /*headMessage*/ undefined, intersectionState);
if (!related) {
if (reportErrors) {
if (reportErrors && (targetArity > 1 || sourceArity > 1)) {
if (i < startCount || i >= targetArity - endCount || sourceArity - startCount - endCount === 1) {
reportIncompatibleError(Diagnostics.Type_at_position_0_in_source_is_not_compatible_with_type_at_position_1_in_target, sourceIndex, i);
}