Skip to content

Commit 74c7c92

Browse files
committed
Try reuse declaredType property cache when possible
1 parent e038290 commit 74c7c92

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/compiler/checker.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16174,7 +16174,7 @@ namespace ts {
1617416174
return result;
1617516175
}
1617616176

16177-
function isMatchingReferenceDiscriminant(expr: Expression, computedType: Type) {
16177+
function isMatchingInFlowDiscriminant(expr: Expression, computedType: Type) {
1617816178
if (!isAccessExpression(expr)) {
1617916179
return false;
1618016180
}
@@ -16184,10 +16184,21 @@ namespace ts {
1618416184
}
1618516185
let propType;
1618616186
return isMatchingReference(reference, expr.expression) &&
16187-
(isDiscriminantProperty(computedType, name) ||
16188-
((computedType.flags & TypeFlags.Union) === 0) &&
16189-
(propType = getTypeOfPropertyOfType(computedType, name))
16190-
&& isUnitType(propType));
16187+
((computedType.flags & TypeFlags.Union) ?
16188+
isDiscriminantProperty(computedType, name) :
16189+
(propType = getTypeOfPropertyOfType(computedType, name)) &&
16190+
isUnitType(propType));
16191+
}
16192+
16193+
function isMatchingReferenceDiscriminant(expr: Expression, computedType: Type) {
16194+
if (!(computedType.flags & TypeFlags.Union) || !isAccessExpression(expr)) {
16195+
return false;
16196+
}
16197+
const name = getAccessedPropertyName(expr);
16198+
if (name === undefined) {
16199+
return false;
16200+
}
16201+
return isMatchingReference(reference, expr.expression) && isDiscriminantProperty(computedType, name);
1619116202
}
1619216203

1619316204
function narrowTypeByDiscriminant(type: Type, access: AccessExpression, narrowType: (t: Type) => Type): Type {
@@ -16255,10 +16266,10 @@ namespace ts {
1625516266
if (isMatchingReference(reference, right)) {
1625616267
return narrowTypeByEquality(type, operator, left, assumeTrue);
1625716268
}
16258-
if (isMatchingReferenceDiscriminant(left, type)) {
16269+
if (isMatchingReferenceDiscriminant(left, declaredType) || isMatchingInFlowDiscriminant(left, type)) {
1625916270
return narrowTypeByDiscriminant(type, <AccessExpression>left, t => narrowTypeByEquality(t, operator, right, assumeTrue));
1626016271
}
16261-
if (isMatchingReferenceDiscriminant(right, type)) {
16272+
if (isMatchingReferenceDiscriminant(left, declaredType) || isMatchingInFlowDiscriminant(left, type)) {
1626216273
return narrowTypeByDiscriminant(type, <AccessExpression>right, t => narrowTypeByEquality(t, operator, left, assumeTrue));
1626316274
}
1626416275
if (containsMatchingReferenceDiscriminant(reference, left) || containsMatchingReferenceDiscriminant(reference, right)) {

0 commit comments

Comments
 (0)