From fe570ba764b011543ae6485de01ce82f3b490487 Mon Sep 17 00:00:00 2001 From: Omer Sheikh Date: Mon, 29 Aug 2016 09:34:46 +0500 Subject: [PATCH] Do not elaborate on primitive type unions --- src/compiler/checker.ts | 6 +++--- tests/baselines/reference/propertyAccess3.errors.txt | 4 +--- .../baselines/reference/typeGuardsWithAny.errors.txt | 2 -- ....errors.txt => unionPropertyExistence.errors.txt} | 12 ++++++------ ...ropertyExistance.js => unionPropertyExistence.js} | 4 ++-- ...ropertyExistance.ts => unionPropertyExistence.ts} | 0 6 files changed, 12 insertions(+), 16 deletions(-) rename tests/baselines/reference/{unionPropertyExistance.errors.txt => unionPropertyExistence.errors.txt} (82%) rename tests/baselines/reference/{unionPropertyExistance.js => unionPropertyExistence.js} (85%) rename tests/cases/compiler/{unionPropertyExistance.ts => unionPropertyExistence.ts} (100%) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 16633d2dbe1..919ba02998e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10773,7 +10773,7 @@ namespace ts { const prop = getPropertyOfType(apparentType, right.text); if (!prop) { if (right.text && !checkAndReportErrorForExtendingInterface(node)) { - reportNonexistantProperty(right, type.flags & TypeFlags.ThisType ? apparentType : type); + reportNonexistentProperty(right, type.flags & TypeFlags.ThisType ? apparentType : type); } return unknownType; } @@ -10811,9 +10811,9 @@ namespace ts { } return getFlowTypeOfReference(node, propType, /*assumeInitialized*/ true, /*flowContainer*/ undefined); - function reportNonexistantProperty(propNode: Identifier, containingType: Type) { + function reportNonexistentProperty(propNode: Identifier, containingType: Type) { let errorInfo: DiagnosticMessageChain; - if (containingType.flags & TypeFlags.Union && !(containingType.flags & TypeFlags.Enum)) { + if (containingType.flags & TypeFlags.Union && !(containingType.flags & TypeFlags.Primitive)) { for (const subtype of (containingType as UnionType).types) { if (!getPropertyOfType(subtype, propNode.text)) { errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1, declarationNameToString(propNode), typeToString(subtype)); diff --git a/tests/baselines/reference/propertyAccess3.errors.txt b/tests/baselines/reference/propertyAccess3.errors.txt index ab3661d5232..dd09b7147f7 100644 --- a/tests/baselines/reference/propertyAccess3.errors.txt +++ b/tests/baselines/reference/propertyAccess3.errors.txt @@ -1,10 +1,8 @@ tests/cases/compiler/propertyAccess3.ts(2,5): error TS2339: Property 'toBAZ' does not exist on type 'boolean'. - Property 'toBAZ' does not exist on type 'true'. ==== tests/cases/compiler/propertyAccess3.ts (1 errors) ==== var foo: boolean; foo.toBAZ(); ~~~~~ -!!! error TS2339: Property 'toBAZ' does not exist on type 'boolean'. -!!! error TS2339: Property 'toBAZ' does not exist on type 'true'. \ No newline at end of file +!!! error TS2339: Property 'toBAZ' does not exist on type 'boolean'. \ No newline at end of file diff --git a/tests/baselines/reference/typeGuardsWithAny.errors.txt b/tests/baselines/reference/typeGuardsWithAny.errors.txt index 2444cb88012..653c89c7554 100644 --- a/tests/baselines/reference/typeGuardsWithAny.errors.txt +++ b/tests/baselines/reference/typeGuardsWithAny.errors.txt @@ -1,7 +1,6 @@ tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts(11,7): error TS2339: Property 'p' does not exist on type 'string'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts(18,7): error TS2339: Property 'p' does not exist on type 'number'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts(25,7): error TS2339: Property 'p' does not exist on type 'boolean'. - Property 'p' does not exist on type 'true'. ==== tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts (3 errors) ==== @@ -36,7 +35,6 @@ tests/cases/conformance/expressions/typeGuards/typeGuardsWithAny.ts(25,7): error x.p; // Error, type any narrowed by primitive type check ~ !!! error TS2339: Property 'p' does not exist on type 'boolean'. -!!! error TS2339: Property 'p' does not exist on type 'true'. } else { x.p; // No error, type unaffected in this branch diff --git a/tests/baselines/reference/unionPropertyExistance.errors.txt b/tests/baselines/reference/unionPropertyExistence.errors.txt similarity index 82% rename from tests/baselines/reference/unionPropertyExistance.errors.txt rename to tests/baselines/reference/unionPropertyExistence.errors.txt index 5dcb6b4d6c3..1fbaa790bd6 100644 --- a/tests/baselines/reference/unionPropertyExistance.errors.txt +++ b/tests/baselines/reference/unionPropertyExistence.errors.txt @@ -1,16 +1,16 @@ -tests/cases/compiler/unionPropertyExistance.ts(24,4): error TS2339: Property 'onlyInB' does not exist on type 'AB'. +tests/cases/compiler/unionPropertyExistence.ts(24,4): error TS2339: Property 'onlyInB' does not exist on type 'AB'. Property 'onlyInB' does not exist on type 'A'. -tests/cases/compiler/unionPropertyExistance.ts(27,5): error TS2339: Property 'notInC' does not exist on type 'ABC'. +tests/cases/compiler/unionPropertyExistence.ts(27,5): error TS2339: Property 'notInC' does not exist on type 'ABC'. Property 'notInC' does not exist on type 'C'. -tests/cases/compiler/unionPropertyExistance.ts(28,4): error TS2339: Property 'notInB' does not exist on type 'AB'. +tests/cases/compiler/unionPropertyExistence.ts(28,4): error TS2339: Property 'notInB' does not exist on type 'AB'. Property 'notInB' does not exist on type 'B'. -tests/cases/compiler/unionPropertyExistance.ts(29,5): error TS2339: Property 'notInB' does not exist on type 'ABC'. +tests/cases/compiler/unionPropertyExistence.ts(29,5): error TS2339: Property 'notInB' does not exist on type 'ABC'. Property 'notInB' does not exist on type 'B'. -tests/cases/compiler/unionPropertyExistance.ts(32,5): error TS2339: Property 'inNone' does not exist on type 'ABC'. +tests/cases/compiler/unionPropertyExistence.ts(32,5): error TS2339: Property 'inNone' does not exist on type 'ABC'. Property 'inNone' does not exist on type 'A'. -==== tests/cases/compiler/unionPropertyExistance.ts (5 errors) ==== +==== tests/cases/compiler/unionPropertyExistence.ts (5 errors) ==== interface A { inAll: string; notInB: string; diff --git a/tests/baselines/reference/unionPropertyExistance.js b/tests/baselines/reference/unionPropertyExistence.js similarity index 85% rename from tests/baselines/reference/unionPropertyExistance.js rename to tests/baselines/reference/unionPropertyExistence.js index 0836032c6db..22931f151f5 100644 --- a/tests/baselines/reference/unionPropertyExistance.js +++ b/tests/baselines/reference/unionPropertyExistence.js @@ -1,4 +1,4 @@ -//// [unionPropertyExistance.ts] +//// [unionPropertyExistence.ts] interface A { inAll: string; notInB: string; @@ -32,7 +32,7 @@ abc.notInB; abc.inAll; // Ok abc.inNone; -//// [unionPropertyExistance.js] +//// [unionPropertyExistence.js] var ab; var abc; ab.onlyInB; diff --git a/tests/cases/compiler/unionPropertyExistance.ts b/tests/cases/compiler/unionPropertyExistence.ts similarity index 100% rename from tests/cases/compiler/unionPropertyExistance.ts rename to tests/cases/compiler/unionPropertyExistence.ts