Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 5 additions & 16 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15360,24 +15360,13 @@ namespace ts {
}

if (!targetType) {
// Target type is type of construct signature
let constructSignatures: ReadonlyArray<Signature> | undefined;
if (getObjectFlags(rightType) & ObjectFlags.Interface) {
constructSignatures = resolveDeclaredMembers(<InterfaceType>rightType).declaredConstructSignatures;
}
else if (getObjectFlags(rightType) & ObjectFlags.Anonymous) {
constructSignatures = getSignaturesOfType(rightType, SignatureKind.Construct);
}
if (constructSignatures && constructSignatures.length) {
targetType = getUnionType(map(constructSignatures, signature => getReturnTypeOfSignature(getErasedSignature(signature))));
}
const constructSignatures = getSignaturesOfType(rightType, SignatureKind.Construct);
targetType = constructSignatures && constructSignatures.length ?
getUnionType(map(constructSignatures, signature => getReturnTypeOfSignature(getErasedSignature(signature)))) :
emptyObjectType;
}

if (targetType) {
return getNarrowedType(type, targetType, assumeTrue, isTypeDerivedFrom);
}

return type;
return getNarrowedType(type, targetType, assumeTrue, isTypeDerivedFrom);
}

function getNarrowedType(type: Type, candidate: Type, assumeTrue: boolean, isRelated: (source: Type, target: Type) => boolean) {
Expand Down
15 changes: 14 additions & 1 deletion tests/baselines/reference/controlFlowInstanceof.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,17 @@ function goo(x: X) {
x.y;
}
x;
}
}

// Repro from #27282

declare const x: (() => void)|null;
declare const ctor: Function;

if (x instanceof ctor) {
x();
}


//// [controlFlowInstanceof.js]
// Repros from #10167
Expand Down Expand Up @@ -181,3 +191,6 @@ function goo(x) {
}
x;
}
if (x instanceof ctor) {
x();
}
18 changes: 18 additions & 0 deletions tests/baselines/reference/controlFlowInstanceof.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,21 @@ function goo(x: X) {
x;
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 90, 13))
}

// Repro from #27282

declare const x: (() => void)|null;
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 100, 13))

declare const ctor: Function;
>ctor : Symbol(ctor, Decl(controlFlowInstanceof.ts, 101, 13))
>Function : Symbol(Function, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))

if (x instanceof ctor) {
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 100, 13))
>ctor : Symbol(ctor, Decl(controlFlowInstanceof.ts, 101, 13))

x();
>x : Symbol(x, Decl(controlFlowInstanceof.ts, 100, 13))
}

40 changes: 30 additions & 10 deletions tests/baselines/reference/controlFlowInstanceof.types
Original file line number Diff line number Diff line change
Expand Up @@ -130,31 +130,31 @@ class C extends A { c: string }
>c : string

function foo(x: A | undefined) {
>foo : (x: A) => void
>x : A
>foo : (x: A | undefined) => void
>x : A | undefined

x; // A | undefined
>x : A
>x : A | undefined

if (x instanceof B || x instanceof C) {
>x instanceof B || x instanceof C : boolean
>x instanceof B : boolean
>x : A
>x : A | undefined
>B : typeof B
>x instanceof C : boolean
>x : A
>x : A | undefined
>C : typeof C

x; // B | C
>x : B | C
}
x; // A | undefined
>x : A
>x : A | undefined

if (x instanceof B && x instanceof C) {
>x instanceof B && x instanceof C : boolean
>x instanceof B : boolean
>x : A
>x : A | undefined
>B : typeof B
>x instanceof C : boolean
>x : B
Expand All @@ -164,11 +164,11 @@ function foo(x: A | undefined) {
>x : B & C
}
x; // A | undefined
>x : A
>x : A | undefined

if (!x) {
>!x : boolean
>x : A
>x : A | undefined

return;
}
Expand Down Expand Up @@ -211,7 +211,7 @@ function foo(x: A | undefined) {

interface X {
x?: string;
>x : string
>x : string | undefined
}

class Y {
Expand Down Expand Up @@ -241,3 +241,23 @@ function goo(x: X) {
x;
>x : X
}

// Repro from #27282

declare const x: (() => void)|null;
>x : (() => void) | null
>null : null

declare const ctor: Function;
>ctor : Function

if (x instanceof ctor) {
>x instanceof ctor : boolean
>x : (() => void) | null
>ctor : Function

x();
>x() : void
>x : () => void
}

12 changes: 11 additions & 1 deletion tests/cases/compiler/controlFlowInstanceof.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @target: es6
// @strictNullChecks: true

// Repros from #10167

Expand Down Expand Up @@ -96,4 +97,13 @@ function goo(x: X) {
x.y;
}
x;
}
}

// Repro from #27282

declare const x: (() => void)|null;
declare const ctor: Function;

if (x instanceof ctor) {
x();
}