Skip to content

Improve error message to suggest indexed access types. #17459

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

Merged
merged 5 commits into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 14 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,21 @@ namespace ts {
function checkAndReportErrorForUsingTypeAsNamespace(errorLocation: Node, name: __String, meaning: SymbolFlags): boolean {
if (meaning === SymbolFlags.Namespace) {
const symbol = resolveSymbol(resolveName(errorLocation, name, SymbolFlags.Type & ~SymbolFlags.Value, /*nameNotFoundMessage*/undefined, /*nameArg*/ undefined));
const parent = errorLocation.parent;
if (symbol) {
if (isQualifiedName(parent)) {
const propName = parent.right.escapedText;
const propType = getPropertyOfType(getDeclaredTypeOfSymbol(symbol), propName);
if (propType) {
error(
parent,
Diagnostics.Cannot_access_0_1_because_0_is_a_type_but_not_a_namespace_Did_you_mean_to_retrieve_the_type_of_the_property_1_in_0_with_0_1,
unescapeLeadingUnderscores(name),
unescapeLeadingUnderscores(propName),
);
return true;
}
}
error(errorLocation, Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_namespace_here, unescapeLeadingUnderscores(name));
return true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2192,6 +2192,10 @@
"category": "Error",
"code": 2712
},
"Cannot access '{0}.{1}' because '{0}' is a type, but not a namespace. Did you mean to retrieve the type of the property '{1}' in '{0}' with '{0}[\"{1}\"]'?": {
"category": "Error",
"code": 2713
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(6,12): error TS2713: Cannot access 'Foo.bar' because 'Foo' is a type, but not a namespace. Did you mean to retrieve the type of the property 'bar' in 'Foo' with 'Foo["bar"]'?
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(14,12): error TS2503: Cannot find namespace 'Foo'.
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(22,12): error TS2713: Cannot access 'Foo.bar' because 'Foo' is a type, but not a namespace. Did you mean to retrieve the type of the property 'bar' in 'Foo' with 'Foo["bar"]'?
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(29,12): error TS2713: Cannot access 'Foo.bar' because 'Foo' is a type, but not a namespace. Did you mean to retrieve the type of the property 'bar' in 'Foo' with 'Foo["bar"]'?
tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts(36,12): error TS2702: 'Foo' only refers to a type, but is being used as a namespace here.


==== tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts (5 errors) ====
namespace Test1 {
export interface Foo {
bar: string;
}

var x: Foo.bar = "";
~~~~~~~
!!! error TS2713: Cannot access 'Foo.bar' because 'Foo' is a type, but not a namespace. Did you mean to retrieve the type of the property 'bar' in 'Foo' with 'Foo["bar"]'?
}

namespace Test2 {
export class Foo {
bar: string;
}

var x: Foo.bar = "";
~~~
!!! error TS2503: Cannot find namespace 'Foo'.
}

namespace Test3 {
export type Foo = {
bar: string;
}

var x: Foo.bar = "";
~~~~~~~
!!! error TS2713: Cannot access 'Foo.bar' because 'Foo' is a type, but not a namespace. Did you mean to retrieve the type of the property 'bar' in 'Foo' with 'Foo["bar"]'?
}

namespace Test4 {
export type Foo = { bar: number }
| { bar: string }

var x: Foo.bar = "";
~~~~~~~
!!! error TS2713: Cannot access 'Foo.bar' because 'Foo' is a type, but not a namespace. Did you mean to retrieve the type of the property 'bar' in 'Foo' with 'Foo["bar"]'?
}

namespace Test5 {
export type Foo = { bar: number }
| { wat: string }

var x: Foo.bar = "";
~~~
!!! error TS2702: 'Foo' only refers to a type, but is being used as a namespace here.
}
66 changes: 66 additions & 0 deletions tests/baselines/reference/errorForUsingPropertyOfTypeAsType01.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//// [errorForUsingPropertyOfTypeAsType01.ts]
namespace Test1 {
export interface Foo {
bar: string;
}

var x: Foo.bar = "";
}

namespace Test2 {
export class Foo {
bar: string;
}

var x: Foo.bar = "";
}

namespace Test3 {
export type Foo = {
bar: string;
}

var x: Foo.bar = "";
}

namespace Test4 {
export type Foo = { bar: number }
| { bar: string }

var x: Foo.bar = "";
}

namespace Test5 {
export type Foo = { bar: number }
| { wat: string }

var x: Foo.bar = "";
}

//// [errorForUsingPropertyOfTypeAsType01.js]
var Test1;
(function (Test1) {
var x = "";
})(Test1 || (Test1 = {}));
var Test2;
(function (Test2) {
var Foo = (function () {
function Foo() {
}
return Foo;
}());
Test2.Foo = Foo;
var x = "";
})(Test2 || (Test2 = {}));
var Test3;
(function (Test3) {
var x = "";
})(Test3 || (Test3 = {}));
var Test4;
(function (Test4) {
var x = "";
})(Test4 || (Test4 = {}));
var Test5;
(function (Test5) {
var x = "";
})(Test5 || (Test5 = {}));
37 changes: 37 additions & 0 deletions tests/cases/compiler/errorForUsingPropertyOfTypeAsType01.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace Test1 {
export interface Foo {
bar: string;
}

var x: Foo.bar = "";
}

namespace Test2 {
export class Foo {
bar: string;
}

var x: Foo.bar = "";
}

namespace Test3 {
export type Foo = {
bar: string;
}

var x: Foo.bar = "";
}

namespace Test4 {
export type Foo = { bar: number }
| { bar: string }

var x: Foo.bar = "";
}

namespace Test5 {
export type Foo = { bar: number }
| { wat: string }

var x: Foo.bar = "";
}