Skip to content

Commit 04c26b7

Browse files
committed
Improve documentation and naming
1 parent 343572e commit 04c26b7

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/compiler/checker.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8701,7 +8701,7 @@ namespace ts {
87018701
let expandingFlags: number;
87028702
let depth = 0;
87038703
let overflow = false;
8704-
let disableWeakTypeErrors = false;
8704+
let disableWeakTypeCheckingForIntersectionConstituents = false;
87058705

87068706
Debug.assert(relation !== identityRelation || !errorNode, "no error reporting in identity checking");
87078707

@@ -8981,20 +8981,27 @@ namespace ts {
89818981
function typeRelatedToEachType(source: Type, target: IntersectionType, reportErrors: boolean): Ternary {
89828982
let result = Ternary.True;
89838983
const targetTypes = target.types;
8984-
const saveDisableWeakTypeErrors = disableWeakTypeErrors;
8985-
disableWeakTypeErrors = true;
8984+
const saveDisableWeakTypeCheckingForIntersectionConstituents = disableWeakTypeCheckingForIntersectionConstituents;
8985+
disableWeakTypeCheckingForIntersectionConstituents = true;
89868986
for (const targetType of targetTypes) {
89878987
const related = isRelatedTo(source, targetType, reportErrors);
89888988
if (!related) {
8989-
disableWeakTypeErrors = saveDisableWeakTypeErrors;
8989+
disableWeakTypeCheckingForIntersectionConstituents = saveDisableWeakTypeCheckingForIntersectionConstituents;
89908990
return Ternary.False;
89918991
}
89928992
result &= related;
89938993
}
8994-
disableWeakTypeErrors = saveDisableWeakTypeErrors;
8994+
disableWeakTypeCheckingForIntersectionConstituents = saveDisableWeakTypeCheckingForIntersectionConstituents;
89958995
return reportAssignmentToWeakIntersection(source, target, reportErrors) ? Ternary.False : result;
89968996
}
89978997

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+
*/
89989005
function reportAssignmentToWeakIntersection(source: Type, target: IntersectionType, reportErrors: boolean) {
89999006
const needsWeakTypeCheck = source !== globalObjectType && getPropertiesOfType(source).length > 0 && every(target.types, isWeakType);
90009007
if (!needsWeakTypeCheck) {
@@ -9352,10 +9359,10 @@ namespace ts {
93529359
}
93539360
return Ternary.False;
93549361
}
9355-
const saveDisableWeakTypeErrors = disableWeakTypeErrors;
9356-
disableWeakTypeErrors = false;
9362+
const saveDisableWeakTypeCheckingForIntersectionConstituents = disableWeakTypeCheckingForIntersectionConstituents;
9363+
disableWeakTypeCheckingForIntersectionConstituents = false;
93579364
const related = isRelatedTo(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp), reportErrors);
9358-
disableWeakTypeErrors = saveDisableWeakTypeErrors;
9365+
disableWeakTypeCheckingForIntersectionConstituents = saveDisableWeakTypeCheckingForIntersectionConstituents;
93599366
if (!related) {
93609367
if (reportErrors) {
93619368
reportError(Diagnostics.Types_of_property_0_are_incompatible, symbolToString(targetProp));
@@ -9381,7 +9388,10 @@ namespace ts {
93819388
}
93829389
}
93839390
}
9384-
if (!foundMatchingProperty && !disableWeakTypeErrors && source !== globalObjectType && getPropertiesOfType(source).length > 0) {
9391+
if (!foundMatchingProperty &&
9392+
!disableWeakTypeCheckingForIntersectionConstituents &&
9393+
source !== globalObjectType &&
9394+
getPropertiesOfType(source).length > 0) {
93859395
if (reportErrors) {
93869396
reportError(Diagnostics.Weak_type_0_has_no_properties_in_common_with_1, typeToString(target), typeToString(source));
93879397
}

0 commit comments

Comments
 (0)