@@ -14340,6 +14340,11 @@ namespace ts {
14340
14340
return false;
14341
14341
}
14342
14342
14343
+ function hasNarrowableDeclaredType(expr: Node) {
14344
+ const type = getDeclaredTypeOfReference(expr);
14345
+ return !!(type && type.flags & TypeFlags.Union);
14346
+ }
14347
+
14343
14348
function findDiscriminantProperties(sourceProperties: Symbol[], target: Type): Symbol[] | undefined {
14344
14349
let result: Symbol[] | undefined;
14345
14350
for (const sourceProperty of sourceProperties) {
@@ -15373,9 +15378,9 @@ namespace ts {
15373
15378
// We have '==', '!=', '====', or !==' operator with 'typeof xxx' and string literal operands
15374
15379
const target = getReferenceCandidate(typeOfExpr.expression);
15375
15380
if (!isMatchingReference(reference, target)) {
15376
- // For a reference of the form 'x.y', a 'typeof x === ...' type guard resets the
15377
- // narrowed type of 'y' to its declared type.
15378
- if (containsMatchingReference(reference, target)) {
15381
+ // For a reference of the form 'x.y', where 'x' has a narrowable declared type, a
15382
+ // 'typeof x === ...' type guard resets the narrowed type of 'y' to its declared type.
15383
+ if (containsMatchingReference(reference, target) && hasNarrowableDeclaredType(target) ) {
15379
15384
return declaredType;
15380
15385
}
15381
15386
return type;
@@ -15516,9 +15521,9 @@ namespace ts {
15516
15521
function narrowTypeByInstanceof(type: Type, expr: BinaryExpression, assumeTrue: boolean): Type {
15517
15522
const left = getReferenceCandidate(expr.left);
15518
15523
if (!isMatchingReference(reference, left)) {
15519
- // For a reference of the form 'x.y', an 'x instanceof T' type guard resets the
15520
- // narrowed type of 'y' to its declared type.
15521
- if (containsMatchingReference(reference, left)) {
15524
+ // For a reference of the form 'x.y', where 'x' has a narrowable declared type, an
15525
+ // 'x instanceof T' type guard resets the narrowed type of 'y' to its declared type.
15526
+ if (containsMatchingReference(reference, left) && hasNarrowableDeclaredType(left) ) {
15522
15527
return declaredType;
15523
15528
}
15524
15529
return type;
0 commit comments