Skip to content

Commit cc987a1

Browse files
Merge pull request microsoft#29896 from dragomirtitian/microsoftGH-29778
Improve error message for using value as type
2 parents 84076a5 + c358b0b commit cc987a1

12 files changed

+57
-41
lines changed

src/compiler/checker.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1518,7 +1518,8 @@ namespace ts {
15181518
!checkAndReportErrorForExtendingInterface(errorLocation) &&
15191519
!checkAndReportErrorForUsingTypeAsNamespace(errorLocation, name, meaning) &&
15201520
!checkAndReportErrorForUsingTypeAsValue(errorLocation, name, meaning) &&
1521-
!checkAndReportErrorForUsingNamespaceModuleAsValue(errorLocation, name, meaning)) {
1521+
!checkAndReportErrorForUsingNamespaceModuleAsValue(errorLocation, name, meaning) &&
1522+
!checkAndReportErrorForUsingValueAsType(errorLocation, name, meaning)) {
15221523
let suggestion: Symbol | undefined;
15231524
if (suggestedNameNotFoundMessage && suggestionCount < maximumSuggestionCount) {
15241525
suggestion = getSuggestedSymbolForNonexistentSymbol(originalLocation, name, meaning);
@@ -1708,6 +1709,17 @@ namespace ts {
17081709
return false;
17091710
}
17101711

1712+
function checkAndReportErrorForUsingValueAsType(errorLocation: Node, name: __String, meaning: SymbolFlags): boolean {
1713+
if (meaning & (SymbolFlags.Type & ~SymbolFlags.Namespace)) {
1714+
const symbol = resolveSymbol(resolveName(errorLocation, name, ~SymbolFlags.Type & SymbolFlags.Value, /*nameNotFoundMessage*/undefined, /*nameArg*/ undefined, /*isUse*/ false));
1715+
if (symbol && !(symbol.flags & SymbolFlags.Namespace)) {
1716+
error(errorLocation, Diagnostics._0_refers_to_a_value_but_is_being_used_as_a_type_here, unescapeLeadingUnderscores(name));
1717+
return true;
1718+
}
1719+
}
1720+
return false;
1721+
}
1722+
17111723
function checkAndReportErrorForUsingTypeAsValue(errorLocation: Node, name: __String, meaning: SymbolFlags): boolean {
17121724
if (meaning & (SymbolFlags.Value & ~SymbolFlags.NamespaceModule)) {
17131725
if (name === "any" || name === "string" || name === "number" || name === "boolean" || name === "never") {

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2581,6 +2581,10 @@
25812581
"category": "Error",
25822582
"code": 2748
25832583
},
2584+
"'{0}' refers to a value, but is being used as a type here.": {
2585+
"category": "Error",
2586+
"code": 2749
2587+
},
25842588

25852589
"Import declaration '{0}' is using private name '{1}'.": {
25862590
"category": "Error",

tests/baselines/reference/allowImportClausesToMergeWithTypes.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/compiler/index.ts(4,1): error TS2693: 'zzz' only refers to a type, but is being used as a value here.
2-
tests/cases/compiler/index.ts(9,10): error TS2304: Cannot find name 'originalZZZ'.
2+
tests/cases/compiler/index.ts(9,10): error TS2749: 'originalZZZ' refers to a value, but is being used as a type here.
33

44

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

3232
const y: originalZZZ = x;
3333
~~~~~~~~~~~
34-
!!! error TS2304: Cannot find name 'originalZZZ'.
34+
!!! error TS2749: 'originalZZZ' refers to a value, but is being used as a type here.

tests/baselines/reference/callOverloads3.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
tests/cases/compiler/callOverloads3.ts(1,10): error TS2300: Duplicate identifier 'Foo'.
2-
tests/cases/compiler/callOverloads3.ts(1,16): error TS2304: Cannot find name 'Foo'.
2+
tests/cases/compiler/callOverloads3.ts(1,16): error TS2749: 'Foo' refers to a value, but is being used as a type here.
33
tests/cases/compiler/callOverloads3.ts(2,10): error TS2300: Duplicate identifier 'Foo'.
44
tests/cases/compiler/callOverloads3.ts(2,10): error TS2391: Function implementation is missing or not immediately following the declaration.
5-
tests/cases/compiler/callOverloads3.ts(2,24): error TS2304: Cannot find name 'Foo'.
5+
tests/cases/compiler/callOverloads3.ts(2,24): error TS2749: 'Foo' refers to a value, but is being used as a type here.
66
tests/cases/compiler/callOverloads3.ts(3,7): error TS2300: Duplicate identifier 'Foo'.
77
tests/cases/compiler/callOverloads3.ts(11,10): error TS2350: Only a void function can be called with the 'new' keyword.
88

@@ -12,14 +12,14 @@ tests/cases/compiler/callOverloads3.ts(11,10): error TS2350: Only a void functio
1212
~~~
1313
!!! error TS2300: Duplicate identifier 'Foo'.
1414
~~~
15-
!!! error TS2304: Cannot find name 'Foo'.
15+
!!! error TS2749: 'Foo' refers to a value, but is being used as a type here.
1616
function Foo(s:string):Foo; // error
1717
~~~
1818
!!! error TS2300: Duplicate identifier 'Foo'.
1919
~~~
2020
!!! error TS2391: Function implementation is missing or not immediately following the declaration.
2121
~~~
22-
!!! error TS2304: Cannot find name 'Foo'.
22+
!!! error TS2749: 'Foo' refers to a value, but is being used as a type here.
2323
class Foo { // error
2424
~~~
2525
!!! error TS2300: Duplicate identifier 'Foo'.

tests/baselines/reference/callOverloads4.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
tests/cases/compiler/callOverloads4.ts(1,10): error TS2300: Duplicate identifier 'Foo'.
2-
tests/cases/compiler/callOverloads4.ts(1,16): error TS2304: Cannot find name 'Foo'.
2+
tests/cases/compiler/callOverloads4.ts(1,16): error TS2749: 'Foo' refers to a value, but is being used as a type here.
33
tests/cases/compiler/callOverloads4.ts(2,10): error TS2300: Duplicate identifier 'Foo'.
44
tests/cases/compiler/callOverloads4.ts(2,10): error TS2391: Function implementation is missing or not immediately following the declaration.
5-
tests/cases/compiler/callOverloads4.ts(2,24): error TS2304: Cannot find name 'Foo'.
5+
tests/cases/compiler/callOverloads4.ts(2,24): error TS2749: 'Foo' refers to a value, but is being used as a type here.
66
tests/cases/compiler/callOverloads4.ts(3,7): error TS2300: Duplicate identifier 'Foo'.
77
tests/cases/compiler/callOverloads4.ts(11,10): error TS2350: Only a void function can be called with the 'new' keyword.
88

@@ -12,14 +12,14 @@ tests/cases/compiler/callOverloads4.ts(11,10): error TS2350: Only a void functio
1212
~~~
1313
!!! error TS2300: Duplicate identifier 'Foo'.
1414
~~~
15-
!!! error TS2304: Cannot find name 'Foo'.
15+
!!! error TS2749: 'Foo' refers to a value, but is being used as a type here.
1616
function Foo(s:string):Foo; // error
1717
~~~
1818
!!! error TS2300: Duplicate identifier 'Foo'.
1919
~~~
2020
!!! error TS2391: Function implementation is missing or not immediately following the declaration.
2121
~~~
22-
!!! error TS2304: Cannot find name 'Foo'.
22+
!!! error TS2749: 'Foo' refers to a value, but is being used as a type here.
2323
class Foo { // error
2424
~~~
2525
!!! error TS2300: Duplicate identifier 'Foo'.

tests/baselines/reference/callOverloads5.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
tests/cases/compiler/callOverloads5.ts(1,10): error TS2300: Duplicate identifier 'Foo'.
2-
tests/cases/compiler/callOverloads5.ts(1,16): error TS2304: Cannot find name 'Foo'.
2+
tests/cases/compiler/callOverloads5.ts(1,16): error TS2749: 'Foo' refers to a value, but is being used as a type here.
33
tests/cases/compiler/callOverloads5.ts(2,10): error TS2300: Duplicate identifier 'Foo'.
44
tests/cases/compiler/callOverloads5.ts(2,10): error TS2391: Function implementation is missing or not immediately following the declaration.
5-
tests/cases/compiler/callOverloads5.ts(2,24): error TS2304: Cannot find name 'Foo'.
5+
tests/cases/compiler/callOverloads5.ts(2,24): error TS2749: 'Foo' refers to a value, but is being used as a type here.
66
tests/cases/compiler/callOverloads5.ts(3,7): error TS2300: Duplicate identifier 'Foo'.
77
tests/cases/compiler/callOverloads5.ts(13,10): error TS2350: Only a void function can be called with the 'new' keyword.
88

@@ -12,14 +12,14 @@ tests/cases/compiler/callOverloads5.ts(13,10): error TS2350: Only a void functio
1212
~~~
1313
!!! error TS2300: Duplicate identifier 'Foo'.
1414
~~~
15-
!!! error TS2304: Cannot find name 'Foo'.
15+
!!! error TS2749: 'Foo' refers to a value, but is being used as a type here.
1616
function Foo(s:string):Foo; // error
1717
~~~
1818
!!! error TS2300: Duplicate identifier 'Foo'.
1919
~~~
2020
!!! error TS2391: Function implementation is missing or not immediately following the declaration.
2121
~~~
22-
!!! error TS2304: Cannot find name 'Foo'.
22+
!!! error TS2749: 'Foo' refers to a value, but is being used as a type here.
2323
class Foo { // error
2424
~~~
2525
!!! error TS2300: Duplicate identifier 'Foo'.

tests/baselines/reference/constructorOverloads7.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tests/cases/compiler/constructorOverloads7.ts(1,15): error TS2300: Duplicate identifier 'Point'.
2-
tests/cases/compiler/constructorOverloads7.ts(7,35): error TS2304: Cannot find name 'Point'.
3-
tests/cases/compiler/constructorOverloads7.ts(8,14): error TS2304: Cannot find name 'Point'.
2+
tests/cases/compiler/constructorOverloads7.ts(7,35): error TS2749: 'Point' refers to a value, but is being used as a type here.
3+
tests/cases/compiler/constructorOverloads7.ts(8,14): error TS2749: 'Point' refers to a value, but is being used as a type here.
44
tests/cases/compiler/constructorOverloads7.ts(15,10): error TS2300: Duplicate identifier 'Point'.
55
tests/cases/compiler/constructorOverloads7.ts(22,18): error TS2384: Overload signatures must all be ambient or non-ambient.
66

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

1717
add(dx: number, dy: number): Point;
1818
~~~~~
19-
!!! error TS2304: Cannot find name 'Point'.
19+
!!! error TS2749: 'Point' refers to a value, but is being used as a type here.
2020
origin: Point;
2121
~~~~~
22-
!!! error TS2304: Cannot find name 'Point'.
22+
!!! error TS2749: 'Point' refers to a value, but is being used as a type here.
2323

2424
}
2525

tests/baselines/reference/intrinsics.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
tests/cases/compiler/intrinsics.ts(1,21): error TS2304: Cannot find name 'hasOwnProperty'.
1+
tests/cases/compiler/intrinsics.ts(1,21): error TS2749: 'hasOwnProperty' refers to a value, but is being used as a type here.
22
tests/cases/compiler/intrinsics.ts(1,21): error TS4025: Exported variable 'hasOwnProperty' has or is using private name 'hasOwnProperty'.
33
tests/cases/compiler/intrinsics.ts(10,1): error TS2304: Cannot find name '__proto__'.
44

55

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

tests/baselines/reference/jsdocTypeNongenericInstantiationAttempt.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ tests/cases/compiler/index4.js(2,19): error TS2315: Type 'Function' is not gener
55
tests/cases/compiler/index5.js(2,19): error TS2315: Type 'String' is not generic.
66
tests/cases/compiler/index6.js(2,19): error TS2315: Type 'Number' is not generic.
77
tests/cases/compiler/index7.js(2,19): error TS2315: Type 'Object' is not generic.
8-
tests/cases/compiler/index8.js(4,12): error TS2304: Cannot find name 'fn'.
8+
tests/cases/compiler/index8.js(4,12): error TS2749: 'fn' refers to a value, but is being used as a type here.
99
tests/cases/compiler/index8.js(4,15): error TS2304: Cannot find name 'T'.
1010

1111

@@ -90,7 +90,7 @@ tests/cases/compiler/index8.js(4,15): error TS2304: Cannot find name 'T'.
9090
/**
9191
* @param {fn<T>} somebody
9292
~~
93-
!!! error TS2304: Cannot find name 'fn'.
93+
!!! error TS2749: 'fn' refers to a value, but is being used as a type here.
9494
~
9595
!!! error TS2304: Cannot find name 'T'.
9696
*/
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tests/cases/conformance/parser/ecmascript5/Generics/parserAmbiguityWithBinaryOperator4.ts(3,9): error TS2347: Untyped function calls may not accept type arguments.
2-
tests/cases/conformance/parser/ecmascript5/Generics/parserAmbiguityWithBinaryOperator4.ts(3,11): error TS2304: Cannot find name 'b'.
3-
tests/cases/conformance/parser/ecmascript5/Generics/parserAmbiguityWithBinaryOperator4.ts(3,14): error TS2304: Cannot find name 'b'.
2+
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.
3+
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.
44

55

66
==== tests/cases/conformance/parser/ecmascript5/Generics/parserAmbiguityWithBinaryOperator4.ts (3 errors) ====
@@ -10,7 +10,7 @@ tests/cases/conformance/parser/ecmascript5/Generics/parserAmbiguityWithBinaryOpe
1010
~~~~~~~~~~~~~~
1111
!!! error TS2347: Untyped function calls may not accept type arguments.
1212
~
13-
!!! error TS2304: Cannot find name 'b'.
13+
!!! error TS2749: 'b' refers to a value, but is being used as a type here.
1414
~
15-
!!! error TS2304: Cannot find name 'b'.
15+
!!! error TS2749: 'b' refers to a value, but is being used as a type here.
1616
}

0 commit comments

Comments
 (0)