From 59d027b49bf1aae2541238f5693cfe560dde75a2 Mon Sep 17 00:00:00 2001 From: Omer Sheikh Date: Sun, 28 Aug 2016 12:29:05 +0500 Subject: [PATCH 1/4] Show elaboration for property not existing in union Fixes #10256. Accessing a non-existant property on union types should now show an elaboration in the error message specifying the first constituent type that lacks the property. --- src/compiler/checker.ts | 16 +++++++++++++++- .../reference/propertyAccess3.errors.txt | 4 +++- .../reference/typeGuardsWithAny.errors.txt | 2 ++ ...thInstanceOfByConstructorSignature.errors.txt | 12 ++++++++++++ .../reference/unionTypeMembers.errors.txt | 10 +++++++++- .../reference/unionTypeReadonly.errors.txt | 2 ++ 6 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8c1e401912d..16633d2dbe1 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)) { - error(right, Diagnostics.Property_0_does_not_exist_on_type_1, declarationNameToString(right), typeToString(type.flags & TypeFlags.ThisType ? apparentType : type)); + reportNonexistantProperty(right, type.flags & TypeFlags.ThisType ? apparentType : type); } return unknownType; } @@ -10810,6 +10810,20 @@ namespace ts { return propType; } return getFlowTypeOfReference(node, propType, /*assumeInitialized*/ true, /*flowContainer*/ undefined); + + function reportNonexistantProperty(propNode: Identifier, containingType: Type) { + let errorInfo: DiagnosticMessageChain; + if (containingType.flags & TypeFlags.Union && !(containingType.flags & TypeFlags.Enum)) { + 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)); + break; + } + } + } + errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1, declarationNameToString(propNode), typeToString(containingType)); + diagnostics.add(createDiagnosticForNodeFromMessageChain(propNode, errorInfo)); + } } function isValidPropertyAccess(node: PropertyAccessExpression | QualifiedName, propertyName: string): boolean { diff --git a/tests/baselines/reference/propertyAccess3.errors.txt b/tests/baselines/reference/propertyAccess3.errors.txt index dd09b7147f7..ab3661d5232 100644 --- a/tests/baselines/reference/propertyAccess3.errors.txt +++ b/tests/baselines/reference/propertyAccess3.errors.txt @@ -1,8 +1,10 @@ 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'. \ No newline at end of file +!!! 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 diff --git a/tests/baselines/reference/typeGuardsWithAny.errors.txt b/tests/baselines/reference/typeGuardsWithAny.errors.txt index 653c89c7554..2444cb88012 100644 --- a/tests/baselines/reference/typeGuardsWithAny.errors.txt +++ b/tests/baselines/reference/typeGuardsWithAny.errors.txt @@ -1,6 +1,7 @@ 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) ==== @@ -35,6 +36,7 @@ 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/typeGuardsWithInstanceOfByConstructorSignature.errors.txt b/tests/baselines/reference/typeGuardsWithInstanceOfByConstructorSignature.errors.txt index b67f7328856..36aa370df25 100644 --- a/tests/baselines/reference/typeGuardsWithInstanceOfByConstructorSignature.errors.txt +++ b/tests/baselines/reference/typeGuardsWithInstanceOfByConstructorSignature.errors.txt @@ -5,14 +5,20 @@ tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstru tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(41,10): error TS2339: Property 'bar' does not exist on type 'B'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(66,10): error TS2339: Property 'bar2' does not exist on type 'C1'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(72,10): error TS2339: Property 'bar1' does not exist on type 'C1 | C2'. + Property 'bar1' does not exist on type 'C2'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(73,10): error TS2339: Property 'bar2' does not exist on type 'C1 | C2'. + Property 'bar2' does not exist on type 'C1'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(85,10): error TS2339: Property 'bar' does not exist on type 'D'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(91,10): error TS2339: Property 'bar' does not exist on type 'D'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(112,10): error TS2339: Property 'bar2' does not exist on type 'E1'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(118,11): error TS2339: Property 'bar1' does not exist on type 'E1 | E2'. + Property 'bar1' does not exist on type 'E2'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(119,11): error TS2339: Property 'bar2' does not exist on type 'E1 | E2'. + Property 'bar2' does not exist on type 'E1'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(134,11): error TS2339: Property 'foo' does not exist on type 'string | F'. + Property 'foo' does not exist on type 'string'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(135,11): error TS2339: Property 'bar' does not exist on type 'string | F'. + Property 'bar' does not exist on type 'string'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(160,11): error TS2339: Property 'foo2' does not exist on type 'G1'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(166,11): error TS2339: Property 'foo2' does not exist on type 'G1'. tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(182,11): error TS2339: Property 'bar' does not exist on type 'H'. @@ -107,9 +113,11 @@ tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstru obj6.bar1; ~~~~ !!! error TS2339: Property 'bar1' does not exist on type 'C1 | C2'. +!!! error TS2339: Property 'bar1' does not exist on type 'C2'. obj6.bar2; ~~~~ !!! error TS2339: Property 'bar2' does not exist on type 'C1 | C2'. +!!! error TS2339: Property 'bar2' does not exist on type 'C1'. } // with object type literal @@ -163,9 +171,11 @@ tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstru obj10.bar1; ~~~~ !!! error TS2339: Property 'bar1' does not exist on type 'E1 | E2'. +!!! error TS2339: Property 'bar1' does not exist on type 'E2'. obj10.bar2; ~~~~ !!! error TS2339: Property 'bar2' does not exist on type 'E1 | E2'. +!!! error TS2339: Property 'bar2' does not exist on type 'E1'. } // a construct signature that returns any @@ -183,9 +193,11 @@ tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstru obj11.foo; ~~~ !!! error TS2339: Property 'foo' does not exist on type 'string | F'. +!!! error TS2339: Property 'foo' does not exist on type 'string'. obj11.bar; ~~~ !!! error TS2339: Property 'bar' does not exist on type 'string | F'. +!!! error TS2339: Property 'bar' does not exist on type 'string'. } var obj12: any; diff --git a/tests/baselines/reference/unionTypeMembers.errors.txt b/tests/baselines/reference/unionTypeMembers.errors.txt index 21f1204ac6d..e4f73a51f77 100644 --- a/tests/baselines/reference/unionTypeMembers.errors.txt +++ b/tests/baselines/reference/unionTypeMembers.errors.txt @@ -1,8 +1,12 @@ tests/cases/conformance/types/union/unionTypeMembers.ts(44,1): error TS2349: Cannot invoke an expression whose type lacks a call signature. tests/cases/conformance/types/union/unionTypeMembers.ts(51,3): error TS2339: Property 'propertyOnlyInI1' does not exist on type 'I1 | I2'. + Property 'propertyOnlyInI1' does not exist on type 'I2'. tests/cases/conformance/types/union/unionTypeMembers.ts(52,3): error TS2339: Property 'propertyOnlyInI2' does not exist on type 'I1 | I2'. + Property 'propertyOnlyInI2' does not exist on type 'I1'. tests/cases/conformance/types/union/unionTypeMembers.ts(53,3): error TS2339: Property 'methodOnlyInI1' does not exist on type 'I1 | I2'. + Property 'methodOnlyInI1' does not exist on type 'I2'. tests/cases/conformance/types/union/unionTypeMembers.ts(54,3): error TS2339: Property 'methodOnlyInI2' does not exist on type 'I1 | I2'. + Property 'methodOnlyInI2' does not exist on type 'I1'. ==== tests/cases/conformance/types/union/unionTypeMembers.ts (5 errors) ==== @@ -61,12 +65,16 @@ tests/cases/conformance/types/union/unionTypeMembers.ts(54,3): error TS2339: Pro x.propertyOnlyInI1; // error ~~~~~~~~~~~~~~~~ !!! error TS2339: Property 'propertyOnlyInI1' does not exist on type 'I1 | I2'. +!!! error TS2339: Property 'propertyOnlyInI1' does not exist on type 'I2'. x.propertyOnlyInI2; // error ~~~~~~~~~~~~~~~~ !!! error TS2339: Property 'propertyOnlyInI2' does not exist on type 'I1 | I2'. +!!! error TS2339: Property 'propertyOnlyInI2' does not exist on type 'I1'. x.methodOnlyInI1("hello"); // error ~~~~~~~~~~~~~~ !!! error TS2339: Property 'methodOnlyInI1' does not exist on type 'I1 | I2'. +!!! error TS2339: Property 'methodOnlyInI1' does not exist on type 'I2'. x.methodOnlyInI2(10); // error ~~~~~~~~~~~~~~ -!!! error TS2339: Property 'methodOnlyInI2' does not exist on type 'I1 | I2'. \ No newline at end of file +!!! error TS2339: Property 'methodOnlyInI2' does not exist on type 'I1 | I2'. +!!! error TS2339: Property 'methodOnlyInI2' does not exist on type 'I1'. \ No newline at end of file diff --git a/tests/baselines/reference/unionTypeReadonly.errors.txt b/tests/baselines/reference/unionTypeReadonly.errors.txt index 0875b2b5af3..ec660fc3a81 100644 --- a/tests/baselines/reference/unionTypeReadonly.errors.txt +++ b/tests/baselines/reference/unionTypeReadonly.errors.txt @@ -3,6 +3,7 @@ tests/cases/conformance/types/union/unionTypeReadonly.ts(19,1): error TS2450: Le tests/cases/conformance/types/union/unionTypeReadonly.ts(21,1): error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property. tests/cases/conformance/types/union/unionTypeReadonly.ts(23,1): error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property. tests/cases/conformance/types/union/unionTypeReadonly.ts(25,15): error TS2339: Property 'value' does not exist on type 'Base | DifferentName'. + Property 'value' does not exist on type 'DifferentName'. ==== tests/cases/conformance/types/union/unionTypeReadonly.ts (5 errors) ==== @@ -41,5 +42,6 @@ tests/cases/conformance/types/union/unionTypeReadonly.ts(25,15): error TS2339: P differentName.value = 12; // error, property 'value' doesn't exist ~~~~~ !!! error TS2339: Property 'value' does not exist on type 'Base | DifferentName'. +!!! error TS2339: Property 'value' does not exist on type 'DifferentName'. \ No newline at end of file From 4eaee735644b2a013b542ada4b50351277f38937 Mon Sep 17 00:00:00 2001 From: Omer Sheikh Date: Mon, 29 Aug 2016 01:07:10 +0500 Subject: [PATCH 2/4] Add test for invalid property access in unions --- .../unionPropertyExistance.errors.txt | 60 +++++++++++++++++++ .../reference/unionPropertyExistance.js | 44 ++++++++++++++ .../cases/compiler/unionPropertyExistance.ts | 32 ++++++++++ 3 files changed, 136 insertions(+) create mode 100644 tests/baselines/reference/unionPropertyExistance.errors.txt create mode 100644 tests/baselines/reference/unionPropertyExistance.js create mode 100644 tests/cases/compiler/unionPropertyExistance.ts diff --git a/tests/baselines/reference/unionPropertyExistance.errors.txt b/tests/baselines/reference/unionPropertyExistance.errors.txt new file mode 100644 index 00000000000..5dcb6b4d6c3 --- /dev/null +++ b/tests/baselines/reference/unionPropertyExistance.errors.txt @@ -0,0 +1,60 @@ +tests/cases/compiler/unionPropertyExistance.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'. + 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'. + 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'. + 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'. + Property 'inNone' does not exist on type 'A'. + + +==== tests/cases/compiler/unionPropertyExistance.ts (5 errors) ==== + interface A { + inAll: string; + notInB: string; + notInC: string; + } + + interface B { + inAll: boolean; + onlyInB: number; + notInC: string; + } + + interface C { + inAll: number; + notInB: string; + } + + type AB = A | B; + type ABC = C | AB; + + var ab: AB; + var abc: ABC; + + ab.onlyInB; + ~~~~~~~ +!!! error TS2339: Property 'onlyInB' does not exist on type 'AB'. +!!! error TS2339: Property 'onlyInB' does not exist on type 'A'. + + ab.notInC; // Ok + abc.notInC; + ~~~~~~ +!!! error TS2339: Property 'notInC' does not exist on type 'ABC'. +!!! error TS2339: Property 'notInC' does not exist on type 'C'. + ab.notInB; + ~~~~~~ +!!! error TS2339: Property 'notInB' does not exist on type 'AB'. +!!! error TS2339: Property 'notInB' does not exist on type 'B'. + abc.notInB; + ~~~~~~ +!!! error TS2339: Property 'notInB' does not exist on type 'ABC'. +!!! error TS2339: Property 'notInB' does not exist on type 'B'. + + abc.inAll; // Ok + abc.inNone; + ~~~~~~ +!!! error TS2339: Property 'inNone' does not exist on type 'ABC'. +!!! error TS2339: Property 'inNone' does not exist on type 'A'. \ No newline at end of file diff --git a/tests/baselines/reference/unionPropertyExistance.js b/tests/baselines/reference/unionPropertyExistance.js new file mode 100644 index 00000000000..0836032c6db --- /dev/null +++ b/tests/baselines/reference/unionPropertyExistance.js @@ -0,0 +1,44 @@ +//// [unionPropertyExistance.ts] +interface A { + inAll: string; + notInB: string; + notInC: string; +} + +interface B { + inAll: boolean; + onlyInB: number; + notInC: string; +} + +interface C { + inAll: number; + notInB: string; +} + +type AB = A | B; +type ABC = C | AB; + +var ab: AB; +var abc: ABC; + +ab.onlyInB; + +ab.notInC; // Ok +abc.notInC; +ab.notInB; +abc.notInB; + +abc.inAll; // Ok +abc.inNone; + +//// [unionPropertyExistance.js] +var ab; +var abc; +ab.onlyInB; +ab.notInC; // Ok +abc.notInC; +ab.notInB; +abc.notInB; +abc.inAll; // Ok +abc.inNone; diff --git a/tests/cases/compiler/unionPropertyExistance.ts b/tests/cases/compiler/unionPropertyExistance.ts new file mode 100644 index 00000000000..d0bee2fb829 --- /dev/null +++ b/tests/cases/compiler/unionPropertyExistance.ts @@ -0,0 +1,32 @@ +interface A { + inAll: string; + notInB: string; + notInC: string; +} + +interface B { + inAll: boolean; + onlyInB: number; + notInC: string; +} + +interface C { + inAll: number; + notInB: string; +} + +type AB = A | B; +type ABC = C | AB; + +var ab: AB; +var abc: ABC; + +ab.onlyInB; + +ab.notInC; // Ok +abc.notInC; +ab.notInB; +abc.notInB; + +abc.inAll; // Ok +abc.inNone; \ No newline at end of file From fe570ba764b011543ae6485de01ce82f3b490487 Mon Sep 17 00:00:00 2001 From: Omer Sheikh Date: Mon, 29 Aug 2016 09:34:46 +0500 Subject: [PATCH 3/4] 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 From f825b9c0f3ae4c89e5d8a8da7b1ba1ff7728028d Mon Sep 17 00:00:00 2001 From: Omer Sheikh Date: Mon, 29 Aug 2016 09:48:17 +0500 Subject: [PATCH 4/4] Add additional tests to unionPropertyExistence --- .../unionPropertyExistence.errors.txt | 38 +++++++++++++++---- .../reference/unionPropertyExistence.js | 15 +++++++- .../cases/compiler/unionPropertyExistence.ts | 10 ++++- 3 files changed, 54 insertions(+), 9 deletions(-) diff --git a/tests/baselines/reference/unionPropertyExistence.errors.txt b/tests/baselines/reference/unionPropertyExistence.errors.txt index 1fbaa790bd6..c13aa7d4d15 100644 --- a/tests/baselines/reference/unionPropertyExistence.errors.txt +++ b/tests/baselines/reference/unionPropertyExistence.errors.txt @@ -1,16 +1,22 @@ -tests/cases/compiler/unionPropertyExistence.ts(24,4): error TS2339: Property 'onlyInB' does not exist on type 'AB'. +tests/cases/compiler/unionPropertyExistence.ts(27,3): error TS2339: Property 'nope' does not exist on type '"foo" | "bar"'. + Property 'nope' does not exist on type '"foo"'. +tests/cases/compiler/unionPropertyExistence.ts(28,6): error TS2339: Property 'onlyInB' does not exist on type 'B | "foo"'. + Property 'onlyInB' does not exist on type '"foo"'. +tests/cases/compiler/unionPropertyExistence.ts(30,6): error TS2339: Property 'length' does not exist on type 'B | "foo"'. + Property 'length' does not exist on type 'B'. +tests/cases/compiler/unionPropertyExistence.ts(32,4): error TS2339: Property 'onlyInB' does not exist on type 'AB'. Property 'onlyInB' does not exist on type 'A'. -tests/cases/compiler/unionPropertyExistence.ts(27,5): error TS2339: Property 'notInC' does not exist on type 'ABC'. +tests/cases/compiler/unionPropertyExistence.ts(35,5): error TS2339: Property 'notInC' does not exist on type 'ABC'. Property 'notInC' does not exist on type 'C'. -tests/cases/compiler/unionPropertyExistence.ts(28,4): error TS2339: Property 'notInB' does not exist on type 'AB'. +tests/cases/compiler/unionPropertyExistence.ts(36,4): error TS2339: Property 'notInB' does not exist on type 'AB'. Property 'notInB' does not exist on type 'B'. -tests/cases/compiler/unionPropertyExistence.ts(29,5): error TS2339: Property 'notInB' does not exist on type 'ABC'. +tests/cases/compiler/unionPropertyExistence.ts(37,5): error TS2339: Property 'notInB' does not exist on type 'ABC'. Property 'notInB' does not exist on type 'B'. -tests/cases/compiler/unionPropertyExistence.ts(32,5): error TS2339: Property 'inNone' does not exist on type 'ABC'. +tests/cases/compiler/unionPropertyExistence.ts(40,5): error TS2339: Property 'inNone' does not exist on type 'ABC'. Property 'inNone' does not exist on type 'A'. -==== tests/cases/compiler/unionPropertyExistence.ts (5 errors) ==== +==== tests/cases/compiler/unionPropertyExistence.ts (8 errors) ==== interface A { inAll: string; notInB: string; @@ -34,6 +40,23 @@ tests/cases/compiler/unionPropertyExistence.ts(32,5): error TS2339: Property 'in var ab: AB; var abc: ABC; + declare const x: "foo" | "bar"; + declare const bFoo: B | "foo"; + + x.nope(); + ~~~~ +!!! error TS2339: Property 'nope' does not exist on type '"foo" | "bar"'. +!!! error TS2339: Property 'nope' does not exist on type '"foo"'. + bFoo.onlyInB; + ~~~~~~~ +!!! error TS2339: Property 'onlyInB' does not exist on type 'B | "foo"'. +!!! error TS2339: Property 'onlyInB' does not exist on type '"foo"'. + x.length; // Ok + bFoo.length; + ~~~~~~ +!!! error TS2339: Property 'length' does not exist on type 'B | "foo"'. +!!! error TS2339: Property 'length' does not exist on type 'B'. + ab.onlyInB; ~~~~~~~ !!! error TS2339: Property 'onlyInB' does not exist on type 'AB'. @@ -57,4 +80,5 @@ tests/cases/compiler/unionPropertyExistence.ts(32,5): error TS2339: Property 'in abc.inNone; ~~~~~~ !!! error TS2339: Property 'inNone' does not exist on type 'ABC'. -!!! error TS2339: Property 'inNone' does not exist on type 'A'. \ No newline at end of file +!!! error TS2339: Property 'inNone' does not exist on type 'A'. + \ No newline at end of file diff --git a/tests/baselines/reference/unionPropertyExistence.js b/tests/baselines/reference/unionPropertyExistence.js index 22931f151f5..80174a3d363 100644 --- a/tests/baselines/reference/unionPropertyExistence.js +++ b/tests/baselines/reference/unionPropertyExistence.js @@ -22,6 +22,14 @@ type ABC = C | AB; var ab: AB; var abc: ABC; +declare const x: "foo" | "bar"; +declare const bFoo: B | "foo"; + +x.nope(); +bFoo.onlyInB; +x.length; // Ok +bFoo.length; + ab.onlyInB; ab.notInC; // Ok @@ -30,11 +38,16 @@ ab.notInB; abc.notInB; abc.inAll; // Ok -abc.inNone; +abc.inNone; + //// [unionPropertyExistence.js] var ab; var abc; +x.nope(); +bFoo.onlyInB; +x.length; // Ok +bFoo.length; ab.onlyInB; ab.notInC; // Ok abc.notInC; diff --git a/tests/cases/compiler/unionPropertyExistence.ts b/tests/cases/compiler/unionPropertyExistence.ts index d0bee2fb829..65937088898 100644 --- a/tests/cases/compiler/unionPropertyExistence.ts +++ b/tests/cases/compiler/unionPropertyExistence.ts @@ -21,6 +21,14 @@ type ABC = C | AB; var ab: AB; var abc: ABC; +declare const x: "foo" | "bar"; +declare const bFoo: B | "foo"; + +x.nope(); +bFoo.onlyInB; +x.length; // Ok +bFoo.length; + ab.onlyInB; ab.notInC; // Ok @@ -29,4 +37,4 @@ ab.notInB; abc.notInB; abc.inAll; // Ok -abc.inNone; \ No newline at end of file +abc.inNone;