Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show an elaboration when accessing a non-existent property of a union type #10582

Merged
merged 4 commits into from
Sep 13, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 15 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -10810,6 +10810,20 @@ namespace ts {
return propType;
}
return getFlowTypeOfReference(node, propType, /*assumeInitialized*/ true, /*flowContainer*/ undefined);

function reportNonexistantProperty(propNode: Identifier, containingType: Type) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling: Nonexistent

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reportNonexistentProperty

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 {
Expand Down
4 changes: 3 additions & 1 deletion tests/baselines/reference/propertyAccess3.errors.txt
Original file line number Diff line number Diff line change
@@ -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'.
!!! error TS2339: Property 'toBAZ' does not exist on type 'boolean'.
!!! error TS2339: Property 'toBAZ' does not exist on type 'true'.
Copy link
Member

@RyanCavanaugh RyanCavanaugh Aug 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks weird - let's exclude TypeFlags.Boolean along with TypeFlags.Enum on line 10814

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eugh.

How about for enums and booleans we don't dive in? Specifically, you can check TypeFlags.Boolean and TypeFlags.Enum.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great minds think and read GitHub on Sunday nights alike

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, TypeFlags.Primitive is probably fine to check too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow GitHub syncs weird-like

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Primitive is probably correct. Let's have a test for

declare const x: "foo" | "bar";
x.nope();

2 changes: 2 additions & 0 deletions tests/baselines/reference/typeGuardsWithAny.errors.txt
Original file line number Diff line number Diff line change
@@ -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) ====
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>'.
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'.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
60 changes: 60 additions & 0 deletions tests/baselines/reference/unionPropertyExistance.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
tests/cases/compiler/unionPropertyExistance.ts(24,4): error TS2339: Property 'onlyInB' does not exist on type 'AB'.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rename the file unionPropertyExistence (notice the e)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be happy to.

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'.
44 changes: 44 additions & 0 deletions tests/baselines/reference/unionPropertyExistance.js
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 9 additions & 1 deletion tests/baselines/reference/unionTypeMembers.errors.txt
Original file line number Diff line number Diff line change
@@ -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<number> | I2<number>'.
Property 'propertyOnlyInI1' does not exist on type 'I2<number>'.
tests/cases/conformance/types/union/unionTypeMembers.ts(52,3): error TS2339: Property 'propertyOnlyInI2' does not exist on type 'I1<number> | I2<number>'.
Property 'propertyOnlyInI2' does not exist on type 'I1<number>'.
tests/cases/conformance/types/union/unionTypeMembers.ts(53,3): error TS2339: Property 'methodOnlyInI1' does not exist on type 'I1<number> | I2<number>'.
Property 'methodOnlyInI1' does not exist on type 'I2<number>'.
tests/cases/conformance/types/union/unionTypeMembers.ts(54,3): error TS2339: Property 'methodOnlyInI2' does not exist on type 'I1<number> | I2<number>'.
Property 'methodOnlyInI2' does not exist on type 'I1<number>'.


==== tests/cases/conformance/types/union/unionTypeMembers.ts (5 errors) ====
Expand Down Expand Up @@ -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<number> | I2<number>'.
!!! error TS2339: Property 'propertyOnlyInI1' does not exist on type 'I2<number>'.
x.propertyOnlyInI2; // error
~~~~~~~~~~~~~~~~
!!! error TS2339: Property 'propertyOnlyInI2' does not exist on type 'I1<number> | I2<number>'.
!!! error TS2339: Property 'propertyOnlyInI2' does not exist on type 'I1<number>'.
x.methodOnlyInI1("hello"); // error
~~~~~~~~~~~~~~
!!! error TS2339: Property 'methodOnlyInI1' does not exist on type 'I1<number> | I2<number>'.
!!! error TS2339: Property 'methodOnlyInI1' does not exist on type 'I2<number>'.
x.methodOnlyInI2(10); // error
~~~~~~~~~~~~~~
!!! error TS2339: Property 'methodOnlyInI2' does not exist on type 'I1<number> | I2<number>'.
!!! error TS2339: Property 'methodOnlyInI2' does not exist on type 'I1<number> | I2<number>'.
!!! error TS2339: Property 'methodOnlyInI2' does not exist on type 'I1<number>'.
2 changes: 2 additions & 0 deletions tests/baselines/reference/unionTypeReadonly.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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) ====
Expand Down Expand Up @@ -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'.


32 changes: 32 additions & 0 deletions tests/cases/compiler/unionPropertyExistance.ts
Original file line number Diff line number Diff line change
@@ -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;