Skip to content

Commit 30f4d03

Browse files
committed
narrow to instanceOf klass not typeof klass
Signed-off-by: Ashley Claymore <acutmore@users.noreply.github.com>
1 parent 45bb73d commit 30f4d03

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22762,8 +22762,12 @@ namespace ts {
2276222762
return type;
2276322763
}
2276422764

22765-
const classTypeForPrivateField = getTypeOfSymbolAtLocation(getSymbolOfNode(klass), klass);
22766-
return getNarrowedType(type, classTypeForPrivateField, assumeTrue, isTypeDerivedFrom);
22765+
const classType = <InterfaceType>getTypeOfSymbolAtLocation(getSymbolOfNode(klass), klass);
22766+
const ctorSigs = getSignaturesOfType(classType, SignatureKind.Construct);
22767+
// TODO(aclaymore): verify assertion is valid
22768+
Debug.assert(ctorSigs.length > 0, "narrowTypeByPrivateIdentifierInInExpression should always find the class signature");
22769+
const instanceType = getReturnTypeOfSignature(ctorSigs[0]);
22770+
return getNarrowedType(type, instanceType, assumeTrue, isTypeSubtypeOf);
2276722771
}
2276822772

2276922773
function narrowTypeByOptionalChainContainment(type: Type, operator: SyntaxKind, value: Expression, assumeTrue: boolean): Type {

tests/baselines/reference/privateNameInInExpression.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ class Foo {
125125
>v : any
126126

127127
const y1 = v; // good y1 is typeof Foo
128-
>y1 : typeof Foo
129-
>v : typeof Foo
128+
>y1 : Foo
129+
>v : Foo
130130

131131
} else {
132132
const y2 = v; // y2 is not typeof Foo
@@ -146,8 +146,8 @@ class Foo {
146146
>v : any
147147

148148
const y1 = v; // Good y1 if typeof Foo
149-
>y1 : typeof Foo
150-
>v : typeof Foo
149+
>y1 : Foo
150+
>v : Foo
151151
}
152152
}
153153
}

0 commit comments

Comments
 (0)