@@ -8701,7 +8701,7 @@ namespace ts {
8701
8701
let expandingFlags: number;
8702
8702
let depth = 0;
8703
8703
let overflow = false;
8704
- let disableWeakTypeErrors = false;
8704
+ let disableWeakTypeCheckingForIntersectionConstituents = false;
8705
8705
8706
8706
Debug.assert(relation !== identityRelation || !errorNode, "no error reporting in identity checking");
8707
8707
@@ -8981,20 +8981,27 @@ namespace ts {
8981
8981
function typeRelatedToEachType(source: Type, target: IntersectionType, reportErrors: boolean): Ternary {
8982
8982
let result = Ternary.True;
8983
8983
const targetTypes = target.types;
8984
- const saveDisableWeakTypeErrors = disableWeakTypeErrors ;
8985
- disableWeakTypeErrors = true;
8984
+ const saveDisableWeakTypeCheckingForIntersectionConstituents = disableWeakTypeCheckingForIntersectionConstituents ;
8985
+ disableWeakTypeCheckingForIntersectionConstituents = true;
8986
8986
for (const targetType of targetTypes) {
8987
8987
const related = isRelatedTo(source, targetType, reportErrors);
8988
8988
if (!related) {
8989
- disableWeakTypeErrors = saveDisableWeakTypeErrors ;
8989
+ disableWeakTypeCheckingForIntersectionConstituents = saveDisableWeakTypeCheckingForIntersectionConstituents ;
8990
8990
return Ternary.False;
8991
8991
}
8992
8992
result &= related;
8993
8993
}
8994
- disableWeakTypeErrors = saveDisableWeakTypeErrors ;
8994
+ disableWeakTypeCheckingForIntersectionConstituents = saveDisableWeakTypeCheckingForIntersectionConstituents ;
8995
8995
return reportAssignmentToWeakIntersection(source, target, reportErrors) ? Ternary.False : result;
8996
8996
}
8997
8997
8998
+ /**
8999
+ * An intersection is weak if all of its constituents are weak. Report an error on assignment to a weak intersection
9000
+ * of a type that doesn't share any property names with it.
9001
+ *
9002
+ * Note: This function could create an anonymous type of the flattened intersection properties and call isRelatedTo,
9003
+ * but this makes React's already-bad weak type errors even more confusing.
9004
+ */
8998
9005
function reportAssignmentToWeakIntersection(source: Type, target: IntersectionType, reportErrors: boolean) {
8999
9006
const needsWeakTypeCheck = source !== globalObjectType && getPropertiesOfType(source).length > 0 && every(target.types, isWeakType);
9000
9007
if (!needsWeakTypeCheck) {
@@ -9352,10 +9359,10 @@ namespace ts {
9352
9359
}
9353
9360
return Ternary.False;
9354
9361
}
9355
- const saveDisableWeakTypeErrors = disableWeakTypeErrors ;
9356
- disableWeakTypeErrors = false;
9362
+ const saveDisableWeakTypeCheckingForIntersectionConstituents = disableWeakTypeCheckingForIntersectionConstituents ;
9363
+ disableWeakTypeCheckingForIntersectionConstituents = false;
9357
9364
const related = isRelatedTo(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp), reportErrors);
9358
- disableWeakTypeErrors = saveDisableWeakTypeErrors ;
9365
+ disableWeakTypeCheckingForIntersectionConstituents = saveDisableWeakTypeCheckingForIntersectionConstituents ;
9359
9366
if (!related) {
9360
9367
if (reportErrors) {
9361
9368
reportError(Diagnostics.Types_of_property_0_are_incompatible, symbolToString(targetProp));
@@ -9381,7 +9388,10 @@ namespace ts {
9381
9388
}
9382
9389
}
9383
9390
}
9384
- if (!foundMatchingProperty && !disableWeakTypeErrors && source !== globalObjectType && getPropertiesOfType(source).length > 0) {
9391
+ if (!foundMatchingProperty &&
9392
+ !disableWeakTypeCheckingForIntersectionConstituents &&
9393
+ source !== globalObjectType &&
9394
+ getPropertiesOfType(source).length > 0) {
9385
9395
if (reportErrors) {
9386
9396
reportError(Diagnostics.Weak_type_0_has_no_properties_in_common_with_1, typeToString(target), typeToString(source));
9387
9397
}
0 commit comments