Skip to content

Improve error message for using value as type #29896

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 3 commits into from
Feb 14, 2019
Merged
Show file tree
Hide file tree
Changes from all 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: 13 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,8 @@ namespace ts {
!checkAndReportErrorForExtendingInterface(errorLocation) &&
!checkAndReportErrorForUsingTypeAsNamespace(errorLocation, name, meaning) &&
!checkAndReportErrorForUsingTypeAsValue(errorLocation, name, meaning) &&
!checkAndReportErrorForUsingNamespaceModuleAsValue(errorLocation, name, meaning)) {
!checkAndReportErrorForUsingNamespaceModuleAsValue(errorLocation, name, meaning) &&
!checkAndReportErrorForUsingValueAsType(errorLocation, name, meaning)) {
let suggestion: Symbol | undefined;
if (suggestedNameNotFoundMessage && suggestionCount < maximumSuggestionCount) {
suggestion = getSuggestedSymbolForNonexistentSymbol(originalLocation, name, meaning);
Expand Down Expand Up @@ -1708,6 +1709,17 @@ namespace ts {
return false;
}

function checkAndReportErrorForUsingValueAsType(errorLocation: Node, name: __String, meaning: SymbolFlags): boolean {
if (meaning & (SymbolFlags.Type & ~SymbolFlags.Namespace)) {
const symbol = resolveSymbol(resolveName(errorLocation, name, ~SymbolFlags.Type & SymbolFlags.Value, /*nameNotFoundMessage*/undefined, /*nameArg*/ undefined, /*isUse*/ false));
if (symbol && !(symbol.flags & SymbolFlags.Namespace)) {
error(errorLocation, Diagnostics._0_refers_to_a_value_but_is_being_used_as_a_type_here, unescapeLeadingUnderscores(name));
return true;
}
}
return false;
}

function checkAndReportErrorForUsingTypeAsValue(errorLocation: Node, name: __String, meaning: SymbolFlags): boolean {
if (meaning & (SymbolFlags.Value & ~SymbolFlags.NamespaceModule)) {
if (name === "any" || name === "string" || name === "number" || name === "boolean" || name === "never") {
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2581,6 +2581,10 @@
"category": "Error",
"code": 2748
},
"'{0}' refers to a value, but is being used as a type here.": {
"category": "Error",
"code": 2749
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/compiler/index.ts(4,1): error TS2693: 'zzz' only refers to a type, but is being used as a value here.
tests/cases/compiler/index.ts(9,10): error TS2304: Cannot find name 'originalZZZ'.
tests/cases/compiler/index.ts(9,10): error TS2749: 'originalZZZ' refers to a value, but is being used as a type here.


==== tests/cases/compiler/b.ts (0 errors) ====
Expand Down Expand Up @@ -31,4 +31,4 @@ tests/cases/compiler/index.ts(9,10): error TS2304: Cannot find name 'originalZZZ

const y: originalZZZ = x;
~~~~~~~~~~~
!!! error TS2304: Cannot find name 'originalZZZ'.
!!! error TS2749: 'originalZZZ' refers to a value, but is being used as a type here.
8 changes: 4 additions & 4 deletions tests/baselines/reference/callOverloads3.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests/cases/compiler/callOverloads3.ts(1,10): error TS2300: Duplicate identifier 'Foo'.
tests/cases/compiler/callOverloads3.ts(1,16): error TS2304: Cannot find name 'Foo'.
tests/cases/compiler/callOverloads3.ts(1,16): error TS2749: 'Foo' refers to a value, but is being used as a type here.
tests/cases/compiler/callOverloads3.ts(2,10): error TS2300: Duplicate identifier 'Foo'.
tests/cases/compiler/callOverloads3.ts(2,10): error TS2391: Function implementation is missing or not immediately following the declaration.
tests/cases/compiler/callOverloads3.ts(2,24): error TS2304: Cannot find name 'Foo'.
tests/cases/compiler/callOverloads3.ts(2,24): error TS2749: 'Foo' refers to a value, but is being used as a type here.
tests/cases/compiler/callOverloads3.ts(3,7): error TS2300: Duplicate identifier 'Foo'.
tests/cases/compiler/callOverloads3.ts(11,10): error TS2350: Only a void function can be called with the 'new' keyword.

Expand All @@ -12,14 +12,14 @@ tests/cases/compiler/callOverloads3.ts(11,10): error TS2350: Only a void functio
~~~
!!! error TS2300: Duplicate identifier 'Foo'.
~~~
!!! error TS2304: Cannot find name 'Foo'.
!!! error TS2749: 'Foo' refers to a value, but is being used as a type here.
function Foo(s:string):Foo; // error
~~~
!!! error TS2300: Duplicate identifier 'Foo'.
~~~
!!! error TS2391: Function implementation is missing or not immediately following the declaration.
~~~
!!! error TS2304: Cannot find name 'Foo'.
!!! error TS2749: 'Foo' refers to a value, but is being used as a type here.
class Foo { // error
~~~
!!! error TS2300: Duplicate identifier 'Foo'.
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/callOverloads4.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests/cases/compiler/callOverloads4.ts(1,10): error TS2300: Duplicate identifier 'Foo'.
tests/cases/compiler/callOverloads4.ts(1,16): error TS2304: Cannot find name 'Foo'.
tests/cases/compiler/callOverloads4.ts(1,16): error TS2749: 'Foo' refers to a value, but is being used as a type here.
tests/cases/compiler/callOverloads4.ts(2,10): error TS2300: Duplicate identifier 'Foo'.
tests/cases/compiler/callOverloads4.ts(2,10): error TS2391: Function implementation is missing or not immediately following the declaration.
tests/cases/compiler/callOverloads4.ts(2,24): error TS2304: Cannot find name 'Foo'.
tests/cases/compiler/callOverloads4.ts(2,24): error TS2749: 'Foo' refers to a value, but is being used as a type here.
tests/cases/compiler/callOverloads4.ts(3,7): error TS2300: Duplicate identifier 'Foo'.
tests/cases/compiler/callOverloads4.ts(11,10): error TS2350: Only a void function can be called with the 'new' keyword.

Expand All @@ -12,14 +12,14 @@ tests/cases/compiler/callOverloads4.ts(11,10): error TS2350: Only a void functio
~~~
!!! error TS2300: Duplicate identifier 'Foo'.
~~~
!!! error TS2304: Cannot find name 'Foo'.
!!! error TS2749: 'Foo' refers to a value, but is being used as a type here.
function Foo(s:string):Foo; // error
~~~
!!! error TS2300: Duplicate identifier 'Foo'.
~~~
!!! error TS2391: Function implementation is missing or not immediately following the declaration.
~~~
!!! error TS2304: Cannot find name 'Foo'.
!!! error TS2749: 'Foo' refers to a value, but is being used as a type here.
class Foo { // error
~~~
!!! error TS2300: Duplicate identifier 'Foo'.
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/callOverloads5.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests/cases/compiler/callOverloads5.ts(1,10): error TS2300: Duplicate identifier 'Foo'.
tests/cases/compiler/callOverloads5.ts(1,16): error TS2304: Cannot find name 'Foo'.
tests/cases/compiler/callOverloads5.ts(1,16): error TS2749: 'Foo' refers to a value, but is being used as a type here.
tests/cases/compiler/callOverloads5.ts(2,10): error TS2300: Duplicate identifier 'Foo'.
tests/cases/compiler/callOverloads5.ts(2,10): error TS2391: Function implementation is missing or not immediately following the declaration.
tests/cases/compiler/callOverloads5.ts(2,24): error TS2304: Cannot find name 'Foo'.
tests/cases/compiler/callOverloads5.ts(2,24): error TS2749: 'Foo' refers to a value, but is being used as a type here.
tests/cases/compiler/callOverloads5.ts(3,7): error TS2300: Duplicate identifier 'Foo'.
tests/cases/compiler/callOverloads5.ts(13,10): error TS2350: Only a void function can be called with the 'new' keyword.

Expand All @@ -12,14 +12,14 @@ tests/cases/compiler/callOverloads5.ts(13,10): error TS2350: Only a void functio
~~~
!!! error TS2300: Duplicate identifier 'Foo'.
~~~
!!! error TS2304: Cannot find name 'Foo'.
!!! error TS2749: 'Foo' refers to a value, but is being used as a type here.
function Foo(s:string):Foo; // error
~~~
!!! error TS2300: Duplicate identifier 'Foo'.
~~~
!!! error TS2391: Function implementation is missing or not immediately following the declaration.
~~~
!!! error TS2304: Cannot find name 'Foo'.
!!! error TS2749: 'Foo' refers to a value, but is being used as a type here.
class Foo { // error
~~~
!!! error TS2300: Duplicate identifier 'Foo'.
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/constructorOverloads7.errors.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests/cases/compiler/constructorOverloads7.ts(1,15): error TS2300: Duplicate identifier 'Point'.
tests/cases/compiler/constructorOverloads7.ts(7,35): error TS2304: Cannot find name 'Point'.
tests/cases/compiler/constructorOverloads7.ts(8,14): error TS2304: Cannot find name 'Point'.
tests/cases/compiler/constructorOverloads7.ts(7,35): error TS2749: 'Point' refers to a value, but is being used as a type here.
tests/cases/compiler/constructorOverloads7.ts(8,14): error TS2749: 'Point' refers to a value, but is being used as a type here.
tests/cases/compiler/constructorOverloads7.ts(15,10): error TS2300: Duplicate identifier 'Point'.
tests/cases/compiler/constructorOverloads7.ts(22,18): error TS2384: Overload signatures must all be ambient or non-ambient.

Expand All @@ -16,10 +16,10 @@ tests/cases/compiler/constructorOverloads7.ts(22,18): error TS2384: Overload sig

add(dx: number, dy: number): Point;
~~~~~
!!! error TS2304: Cannot find name 'Point'.
!!! error TS2749: 'Point' refers to a value, but is being used as a type here.
origin: Point;
~~~~~
!!! error TS2304: Cannot find name 'Point'.
!!! error TS2749: 'Point' refers to a value, but is being used as a type here.

}

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/intrinsics.errors.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
tests/cases/compiler/intrinsics.ts(1,21): error TS2304: Cannot find name 'hasOwnProperty'.
tests/cases/compiler/intrinsics.ts(1,21): error TS2749: 'hasOwnProperty' refers to a value, but is being used as a type here.
tests/cases/compiler/intrinsics.ts(1,21): error TS4025: Exported variable 'hasOwnProperty' has or is using private name 'hasOwnProperty'.
tests/cases/compiler/intrinsics.ts(10,1): error TS2304: Cannot find name '__proto__'.


==== tests/cases/compiler/intrinsics.ts (3 errors) ====
var hasOwnProperty: hasOwnProperty; // Error
~~~~~~~~~~~~~~
!!! error TS2304: Cannot find name 'hasOwnProperty'.
!!! error TS2749: 'hasOwnProperty' refers to a value, but is being used as a type here.
~~~~~~~~~~~~~~
!!! error TS4025: Exported variable 'hasOwnProperty' has or is using private name 'hasOwnProperty'.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ tests/cases/compiler/index4.js(2,19): error TS2315: Type 'Function' is not gener
tests/cases/compiler/index5.js(2,19): error TS2315: Type 'String' is not generic.
tests/cases/compiler/index6.js(2,19): error TS2315: Type 'Number' is not generic.
tests/cases/compiler/index7.js(2,19): error TS2315: Type 'Object' is not generic.
tests/cases/compiler/index8.js(4,12): error TS2304: Cannot find name 'fn'.
tests/cases/compiler/index8.js(4,12): error TS2749: 'fn' refers to a value, but is being used as a type here.
tests/cases/compiler/index8.js(4,15): error TS2304: Cannot find name 'T'.


Expand Down Expand Up @@ -90,7 +90,7 @@ tests/cases/compiler/index8.js(4,15): error TS2304: Cannot find name 'T'.
/**
* @param {fn<T>} somebody
~~
!!! error TS2304: Cannot find name 'fn'.
!!! error TS2749: 'fn' refers to a value, but is being used as a type here.
~
!!! error TS2304: Cannot find name 'T'.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests/cases/conformance/parser/ecmascript5/Generics/parserAmbiguityWithBinaryOperator4.ts(3,9): error TS2347: Untyped function calls may not accept type arguments.
tests/cases/conformance/parser/ecmascript5/Generics/parserAmbiguityWithBinaryOperator4.ts(3,11): error TS2304: Cannot find name 'b'.
tests/cases/conformance/parser/ecmascript5/Generics/parserAmbiguityWithBinaryOperator4.ts(3,14): error TS2304: Cannot find name 'b'.
tests/cases/conformance/parser/ecmascript5/Generics/parserAmbiguityWithBinaryOperator4.ts(3,11): error TS2749: 'b' refers to a value, but is being used as a type here.
tests/cases/conformance/parser/ecmascript5/Generics/parserAmbiguityWithBinaryOperator4.ts(3,14): error TS2749: 'b' refers to a value, but is being used as a type here.


==== tests/cases/conformance/parser/ecmascript5/Generics/parserAmbiguityWithBinaryOperator4.ts (3 errors) ====
Expand All @@ -10,7 +10,7 @@ tests/cases/conformance/parser/ecmascript5/Generics/parserAmbiguityWithBinaryOpe
~~~~~~~~~~~~~~
!!! error TS2347: Untyped function calls may not accept type arguments.
~
!!! error TS2304: Cannot find name 'b'.
!!! error TS2749: 'b' refers to a value, but is being used as a type here.
~
!!! error TS2304: Cannot find name 'b'.
!!! error TS2749: 'b' refers to a value, but is being used as a type here.
}
8 changes: 4 additions & 4 deletions tests/baselines/reference/typeAssertions.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ tests/cases/conformance/expressions/typeAssertions/typeAssertions.ts(37,13): err
Property 'q' is missing in type 'SomeDerived' but required in type 'SomeOther'.
tests/cases/conformance/expressions/typeAssertions/typeAssertions.ts(38,13): error TS2352: Conversion of type 'SomeBase' to type 'SomeOther' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
Property 'q' is missing in type 'SomeBase' but required in type 'SomeOther'.
tests/cases/conformance/expressions/typeAssertions/typeAssertions.ts(44,5): error TS2304: Cannot find name 'numOrStr'.
tests/cases/conformance/expressions/typeAssertions/typeAssertions.ts(44,5): error TS2749: 'numOrStr' refers to a value, but is being used as a type here.
tests/cases/conformance/expressions/typeAssertions/typeAssertions.ts(44,14): error TS1005: '>' expected.
tests/cases/conformance/expressions/typeAssertions/typeAssertions.ts(44,14): error TS2304: Cannot find name 'is'.
tests/cases/conformance/expressions/typeAssertions/typeAssertions.ts(44,17): error TS1005: ')' expected.
tests/cases/conformance/expressions/typeAssertions/typeAssertions.ts(44,17): error TS2693: 'string' only refers to a type, but is being used as a value here.
tests/cases/conformance/expressions/typeAssertions/typeAssertions.ts(44,48): error TS1005: ';' expected.
tests/cases/conformance/expressions/typeAssertions/typeAssertions.ts(45,2): error TS2322: Type 'string | number' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.
tests/cases/conformance/expressions/typeAssertions/typeAssertions.ts(48,32): error TS2304: Cannot find name 'numOrStr'.
tests/cases/conformance/expressions/typeAssertions/typeAssertions.ts(48,32): error TS2749: 'numOrStr' refers to a value, but is being used as a type here.
tests/cases/conformance/expressions/typeAssertions/typeAssertions.ts(48,41): error TS1005: ')' expected.
tests/cases/conformance/expressions/typeAssertions/typeAssertions.ts(48,41): error TS2304: Cannot find name 'is'.
tests/cases/conformance/expressions/typeAssertions/typeAssertions.ts(48,44): error TS1005: ';' expected.
Expand Down Expand Up @@ -86,7 +86,7 @@ tests/cases/conformance/expressions/typeAssertions/typeAssertions.ts(48,50): err
var str: string;
if(<numOrStr is string>(numOrStr === undefined)) { // Error
~~~~~~~~
!!! error TS2304: Cannot find name 'numOrStr'.
!!! error TS2749: 'numOrStr' refers to a value, but is being used as a type here.
~~
!!! error TS1005: '>' expected.
~~
Expand All @@ -105,7 +105,7 @@ tests/cases/conformance/expressions/typeAssertions/typeAssertions.ts(48,50): err

if((numOrStr === undefined) as numOrStr is string) { // Error
~~~~~~~~
!!! error TS2304: Cannot find name 'numOrStr'.
!!! error TS2749: 'numOrStr' refers to a value, but is being used as a type here.
~~
!!! error TS1005: ')' expected.
~~
Expand Down
Loading