Merge pull request #27370 from Microsoft/fixInstanceOfFunction

Fix instanceof with type Function for right argument
This commit is contained in:
Anders Hejlsberg 2018-10-03 10:59:50 -07:00 committed by GitHub
commit 81f8b47e5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 78 additions and 28 deletions

View File

@ -15391,24 +15391,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) {

View File

@ -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
@ -181,3 +191,6 @@ function goo(x) {
}
x;
}
if (x instanceof ctor) {
x();
}

View File

@ -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))
}

View File

@ -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
@ -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;
}
@ -211,7 +211,7 @@ function foo(x: A | undefined) {
interface X {
x?: string;
>x : string
>x : string | undefined
}
class Y {
@ -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
}

View File

@ -1,4 +1,5 @@
// @target: es6
// @strictNullChecks: true
// Repros from #10167
@ -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();
}