From ac9b16cff351909866c23155f2d6c4b9d8f534c1 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Wed, 6 May 2015 20:11:31 -0700 Subject: [PATCH] refactor narrowTypeByInstanceof --- src/compiler/checker.ts | 54 ++++++++++--------- ...nstanceOfByConstructorSignature.errors.txt | 16 ++++-- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4c5712299da..d43661c97b6 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5384,38 +5384,42 @@ module ts { if (!isTypeSubtypeOf(rightType, globalFunctionType)) { return type; } - // Target type is type of prototype property + + let targetType: Type; let prototypeProperty = getPropertyOfType(rightType, "prototype"); if (prototypeProperty) { - let targetType = getTypeOfSymbol(prototypeProperty); - if (targetType !== anyType) { - // Narrow to the target type if it's a subtype of the current type - if (isTypeSubtypeOf(targetType, type)) { - return targetType; - } - // If the current type is a union type, remove all constituents that aren't subtypes of the target. - if (type.flags & TypeFlags.Union) { - return getUnionType(filter((type).types, t => isTypeSubtypeOf(t, targetType))); - } + // Target type is type of the protoype property + let prototypePropertyType = getTypeOfSymbol(prototypeProperty); + if (prototypePropertyType !== anyType) { + targetType = prototypePropertyType; } } - // Target type is type of construct signature - let constructSignatures: Signature[]; - if (rightType.flags & TypeFlags.Interface) { - constructSignatures = resolveDeclaredMembers(rightType).declaredConstructSignatures; - } - else if (rightType.flags & TypeFlags.Anonymous) { - constructSignatures = getSignaturesOfType(rightType, SignatureKind.Construct); - } - if (constructSignatures && constructSignatures.length !== 0) { - let instanceType = getUnionType(map(constructSignatures, signature => getReturnTypeOfSignature(getErasedSignature(signature)))); - // Pickup type from union types - if (type.flags & TypeFlags.Union) { - return getUnionType(filter((type).types, t => isTypeSubtypeOf(t, instanceType))); + if (!targetType) { + // Target type is type of construct signature + let constructSignatures: Signature[]; + if (rightType.flags & TypeFlags.Interface) { + constructSignatures = resolveDeclaredMembers(rightType).declaredConstructSignatures; + } + else if (rightType.flags & TypeFlags.Anonymous) { + constructSignatures = getSignaturesOfType(rightType, SignatureKind.Construct); + } + if (constructSignatures && constructSignatures.length) { + targetType = getUnionType(map(constructSignatures, signature => getReturnTypeOfSignature(getErasedSignature(signature)))); } - return instanceType; } + + if (targetType) { + // Narrow to the target type if it's a subtype of the current type + if (isTypeSubtypeOf(targetType, type)) { + return targetType; + } + // If the current type is a union type, remove all constituents that aren't subtypes of the target. + if (type.flags & TypeFlags.Union) { + return getUnionType(filter((type).types, t => isTypeSubtypeOf(t, targetType))); + } + } + return type; } diff --git a/tests/baselines/reference/typeGuardsWithInstanceOfByConstructorSignature.errors.txt b/tests/baselines/reference/typeGuardsWithInstanceOfByConstructorSignature.errors.txt index b963b2c33ae..be1a0e5a2c3 100644 --- a/tests/baselines/reference/typeGuardsWithInstanceOfByConstructorSignature.errors.txt +++ b/tests/baselines/reference/typeGuardsWithInstanceOfByConstructorSignature.errors.txt @@ -1,16 +1,18 @@ tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(12,10): error TS2339: Property 'bar' does not exist on type 'A'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(33,5): error TS2322: Type 'string' is not assignable to type 'number'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(34,10): error TS2339: Property 'bar' does not exist on type 'B'. -tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(63,10): error TS2339: Property 'bar2' does not exist on type 'C1'. +tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(62,10): error TS2339: Property 'bar1' does not exist on type 'C1 | C2'. +tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(63,10): error TS2339: Property 'bar2' does not exist on type 'C1 | C2'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(82,10): error TS2339: Property 'bar' does not exist on type 'D'. -tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(109,10): error TS2339: Property 'bar2' does not exist on type 'E1'. +tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(108,10): error TS2339: Property 'bar1' does not exist on type 'E1 | E2'. +tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(109,10): error TS2339: Property 'bar2' does not exist on type 'E1 | E2'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(131,11): error TS2339: Property 'foo' does not exist on type 'string | F'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(132,11): error TS2339: Property 'bar' does not exist on type 'string | F'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(157,11): error TS2339: Property 'foo2' does not exist on type 'G1'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(179,11): error TS2339: Property 'bar' does not exist on type 'H'. -==== tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts (10 errors) ==== +==== tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts (12 errors) ==== interface AConstructor { new (): A; } @@ -79,9 +81,11 @@ tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstru if (obj5 instanceof C) { // narrowed to C1. obj5.foo; obj5.bar1; + ~~~~ +!!! error TS2339: Property 'bar1' does not exist on type 'C1 | C2'. obj5.bar2; ~~~~ -!!! error TS2339: Property 'bar2' does not exist on type 'C1'. +!!! error TS2339: Property 'bar2' does not exist on type 'C1 | C2'. } var obj6: any; @@ -129,9 +133,11 @@ tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstru if (obj9 instanceof E) { // narrowed to E1. obj9.foo; obj9.bar1; + ~~~~ +!!! error TS2339: Property 'bar1' does not exist on type 'E1 | E2'. obj9.bar2; ~~~~ -!!! error TS2339: Property 'bar2' does not exist on type 'E1'. +!!! error TS2339: Property 'bar2' does not exist on type 'E1 | E2'. } var obj10: any;