Skip to content

improve error message for using property of type as type #45354

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
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3148,6 +3148,30 @@ namespace ts {
return symbol.parent ? getFullyQualifiedName(symbol.parent, containingLocation) + "." + symbolToString(symbol) : symbolToString(symbol, containingLocation, /*meaning*/ undefined, SymbolFormatFlags.DoNotIncludeSymbolChain | SymbolFormatFlags.AllowAnyNodeKind);
}

function getFullQualifiedName(node: QualifiedName) {
while(isQualifiedName(node.parent)) {
node = node.parent;
}
return node;
}

function tryGetQualifiedNameAsValue(node: QualifiedName) {
let left: Identifier | QualifiedName = getFirstIdentifier(node);
let symbol = resolveName(left, left.escapedText, SymbolFlags.Value, undefined, left, /*isUse*/ true);
if (!symbol) {
return undefined;
}
while (isQualifiedName(left.parent)) {
const type = getTypeOfSymbol(symbol);
symbol = getPropertyOfType(type, left.parent.right.escapedText);
if (!symbol) {
return undefined;
}
left = left.parent;
}
return symbol;
}

/**
* Resolves a qualified name and any involved aliases.
*/
Expand Down Expand Up @@ -3199,9 +3223,14 @@ namespace ts {
const declarationName = declarationNameToString(right);
const suggestionForNonexistentModule = getSuggestedSymbolForNonexistentModule(right, namespace);
const exportedTypeSymbol = getMergedSymbol(getSymbol(getExportsOfSymbol(namespace), right.escapedText, SymbolFlags.Type));
const fullQualifiedName = isQualifiedName(name) && getFullQualifiedName(name);
const canSuggestTypeof = fullQualifiedName && !isTypeOfExpression(fullQualifiedName.parent) && tryGetQualifiedNameAsValue(fullQualifiedName);
if (suggestionForNonexistentModule) {
error(right, Diagnostics._0_has_no_exported_member_named_1_Did_you_mean_2, namespaceName, declarationName, symbolToString(suggestionForNonexistentModule));
}
else if (canSuggestTypeof) {
error(name, Diagnostics._0_refers_to_a_value_but_is_being_used_as_a_type_here_Did_you_mean_typeof_0, entityNameToString(fullQualifiedName as QualifiedName));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haven't tried TypeScript 4.4, so I wrote fullQualifiedName as QualifiedName. After upgrading vscode, I find it is unnecessary. The CFA is really impressive, kudos to you guys!

}
else if (meaning & SymbolFlags.Namespace && exportedTypeSymbol && isQualifiedName(name.parent)) {
error(name.parent.right, 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, symbolToString(exportedTypeSymbol), unescapeLeadingUnderscores(name.parent.right.escapedText));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/compiler/errorForUsingPropertyOfTypeAsType03.ts(11,23): error TS2713: Cannot access 'Red.toString' because 'Red' is a type, but not a namespace. Did you mean to retrieve the type of the property 'toString' in 'Red' with 'Red["toString"]'?
tests/cases/compiler/errorForUsingPropertyOfTypeAsType03.ts(11,13): error TS2749: 'Color.Red.toString' refers to a value, but is being used as a type here. Did you mean 'typeof Color.Red.toString'?
tests/cases/compiler/errorForUsingPropertyOfTypeAsType03.ts(13,19): error TS2339: Property 'Red' does not exist on type 'Color'.
tests/cases/compiler/errorForUsingPropertyOfTypeAsType03.ts(19,13): error TS2702: 'C1' only refers to a type, but is being used as a namespace here.
tests/cases/compiler/errorForUsingPropertyOfTypeAsType03.ts(20,13): error TS2702: 'C1' only refers to a type, but is being used as a namespace here.
Expand All @@ -19,8 +19,8 @@ tests/cases/compiler/errorForUsingPropertyOfTypeAsType03.ts(24,13): error TS2713
type C2 = typeof Color;

let a1: Color.Red.toString;
~~~~~~~~
!!! error TS2713: Cannot access 'Red.toString' because 'Red' is a type, but not a namespace. Did you mean to retrieve the type of the property 'toString' in 'Red' with 'Red["toString"]'?
~~~~~~~~~
!!! error TS2749: 'Color.Red.toString' refers to a value, but is being used as a type here. Did you mean 'typeof Color.Red.toString'?
let a2: Color.Red["toString"];
let a3: Color["Red"]["toString"];
~~~~~
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/genericFunduleInModule.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/compiler/genericFunduleInModule.ts(8,10): error TS2694: Namespace 'A' has no exported member 'B'.
tests/cases/compiler/genericFunduleInModule.ts(8,8): error TS2749: 'A.B' refers to a value, but is being used as a type here. Did you mean 'typeof A.B'?


==== tests/cases/compiler/genericFunduleInModule.ts (1 errors) ====
Expand All @@ -10,6 +10,6 @@ tests/cases/compiler/genericFunduleInModule.ts(8,10): error TS2694: Namespace 'A
}

var b: A.B;
~
!!! error TS2694: Namespace 'A' has no exported member 'B'.
~~~
!!! error TS2749: 'A.B' refers to a value, but is being used as a type here. Did you mean 'typeof A.B'?
A.B(1);
6 changes: 3 additions & 3 deletions tests/baselines/reference/genericFunduleInModule2.errors.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tests/cases/compiler/genericFunduleInModule2.ts(11,10): error TS2694: Namespace 'A' has no exported member 'B'.
tests/cases/compiler/genericFunduleInModule2.ts(11,8): error TS2749: 'A.B' refers to a value, but is being used as a type here. Did you mean 'typeof A.B'?


==== tests/cases/compiler/genericFunduleInModule2.ts (1 errors) ====
Expand All @@ -13,6 +13,6 @@ tests/cases/compiler/genericFunduleInModule2.ts(11,10): error TS2694: Namespace
}

var b: A.B;
~
!!! error TS2694: Namespace 'A' has no exported member 'B'.
~~~
!!! error TS2749: 'A.B' refers to a value, but is being used as a type here. Did you mean 'typeof A.B'?
A.B(1);
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/b.ts(2,1): error TS1361: 'types' cannot be used as a value because it was imported using 'import type'.
/b.ts(3,1): error TS1361: 'types' cannot be used as a value because it was imported using 'import type'.
/b.ts(4,14): error TS2694: Namespace '"/a"' has no exported member 'Value'.
/b.ts(4,8): error TS2749: 'types.Value' refers to a value, but is being used as a type here. Did you mean 'typeof types.Value'?
/b.ts(5,7): error TS2741: Property 'a' is missing in type '{}' but required in type 'A'.
/b.ts(6,7): error TS2741: Property 'b' is missing in type '{}' but required in type 'B'.
/b.ts(8,13): error TS1361: 'types' cannot be used as a value because it was imported using 'import type'.
Expand All @@ -23,8 +23,8 @@
!!! error TS1361: 'types' cannot be used as a value because it was imported using 'import type'.
!!! related TS1376 /b.ts:1:18: 'types' was imported here.
let v: types.Value;
~~~~~
!!! error TS2694: Namespace '"/a"' has no exported member 'Value'.
~~~~~~~~~~~
!!! error TS2749: 'types.Value' refers to a value, but is being used as a type here. Did you mean 'typeof types.Value'?
const a: types.A = {};
~
!!! error TS2741: Property 'a' is missing in type '{}' but required in type 'A'.
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/parserharness.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ tests/cases/conformance/parser/ecmascript5/RealWorld/parserharness.ts(16,21): er
tests/cases/conformance/parser/ecmascript5/RealWorld/parserharness.ts(17,21): error TS6053: File 'tests/cases/conformance/parser/ecmascript5/compiler/typescript.ts' not found.
tests/cases/conformance/parser/ecmascript5/RealWorld/parserharness.ts(18,21): error TS6053: File 'tests/cases/conformance/parser/ecmascript5/services/typescriptServices.ts' not found.
tests/cases/conformance/parser/ecmascript5/RealWorld/parserharness.ts(19,21): error TS6053: File 'tests/cases/conformance/parser/ecmascript5/RealWorld/diff.ts' not found.
tests/cases/conformance/parser/ecmascript5/RealWorld/parserharness.ts(21,29): error TS2694: Namespace 'Harness' has no exported member 'Assert'.
tests/cases/conformance/parser/ecmascript5/RealWorld/parserharness.ts(21,21): error TS2749: 'Harness.Assert' refers to a value, but is being used as a type here. Did you mean 'typeof Harness.Assert'?
tests/cases/conformance/parser/ecmascript5/RealWorld/parserharness.ts(25,17): error TS2304: Cannot find name 'IIO'.
tests/cases/conformance/parser/ecmascript5/RealWorld/parserharness.ts(41,12): error TS2304: Cannot find name 'ActiveXObject'.
tests/cases/conformance/parser/ecmascript5/RealWorld/parserharness.ts(43,19): error TS2580: Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
Expand Down Expand Up @@ -140,8 +140,8 @@ tests/cases/conformance/parser/ecmascript5/RealWorld/parserharness.ts(2030,32):
!!! error TS6053: File 'tests/cases/conformance/parser/ecmascript5/RealWorld/diff.ts' not found.

declare var assert: Harness.Assert;
~~~~~~
!!! error TS2694: Namespace 'Harness' has no exported member 'Assert'.
~~~~~~~~~~~~~~
!!! error TS2749: 'Harness.Assert' refers to a value, but is being used as a type here. Did you mean 'typeof Harness.Assert'?
declare var it;
declare var describe;
declare var run;
Expand Down