Skip to content

Commit 03182bf

Browse files
committed
Update error message + refactor
1 parent 43c14e6 commit 03182bf

24 files changed

+86
-95
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ namespace ts {
872872
if (!errorLocation ||
873873
!checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg) &&
874874
!checkAndReportErrorForExtendingInterface(errorLocation) &&
875-
!checkAndReportErrorForUsingTypeAsValue(errorLocation, name, meaning, nameNotFoundMessage, nameArg)) {
875+
!checkAndReportErrorForUsingTypeAsValue(errorLocation, name, meaning)) {
876876
error(errorLocation, nameNotFoundMessage, typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg));
877877
}
878878
}
@@ -982,28 +982,15 @@ namespace ts {
982982
}
983983
}
984984

985-
function checkAndReportErrorForUsingTypeAsValue(errorLocation: Node, name: string, meaning: SymbolFlags, nameNotFoundMessage: DiagnosticMessage, nameArg: string | Identifier): boolean {
986-
const strictlyValueMeanings = SymbolFlags.Value & ~SymbolFlags.Type;
987-
const strictlyTypeMeanings = SymbolFlags.Type & ~SymbolFlags.Value;
988-
989-
if (!(meaning & strictlyValueMeanings) || meaning & SymbolFlags.NamespaceModule) {
990-
return false;
991-
}
992-
993-
const nameAsType = resolveName(errorLocation, name, strictlyTypeMeanings, nameNotFoundMessage, nameArg);
994-
if (!nameAsType) {
995-
return false;
996-
}
997-
998-
if (nameAsType.flags & SymbolFlags.Alias) {
999-
const resolvedSymbol = resolveAlias(nameAsType);
1000-
if (resolvedSymbol.flags & SymbolFlags.NamespaceModule) {
1001-
return false;
985+
function checkAndReportErrorForUsingTypeAsValue(errorLocation: Node, name: string, meaning: SymbolFlags): boolean {
986+
if (meaning & (SymbolFlags.Value & ~SymbolFlags.NamespaceModule)) {
987+
const symbol = resolveSymbol(resolveName(errorLocation, name, SymbolFlags.Type & ~SymbolFlags.Value, /*nameNotFoundMessage*/undefined, /*nameArg*/ undefined));
988+
if (symbol && !(symbol.flags & SymbolFlags.NamespaceModule)) {
989+
error(errorLocation, Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here, name);
990+
return true;
1002991
}
1003992
}
1004-
1005-
error(errorLocation, Diagnostics.Cannot_find_name_0_A_type_exists_with_this_name_but_no_value, name);
1006-
return true;
993+
return false;
1007994
}
1008995

1009996
function checkResolvedBlockScopedVariable(result: Symbol, errorLocation: Node): void {

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,6 +1959,10 @@
19591959
"category": "Error",
19601960
"code": 2692
19611961
},
1962+
"'{0}' only refers to a type, but is being used as a value here.": {
1963+
"category": "Error",
1964+
"code": 2693
1965+
},
19621966
"Import declaration '{0}' is using private name '{1}'.": {
19631967
"category": "Error",
19641968
"code": 4000

tests/baselines/reference/assignments.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(14,1): er
33
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(17,1): error TS2364: Invalid left-hand side of assignment expression.
44
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(18,1): error TS2450: Left-hand side of assignment expression cannot be a constant or a read-only property.
55
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(21,1): error TS2364: Invalid left-hand side of assignment expression.
6-
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(31,1): error TS2692: Cannot find name 'I'. A type exists with this name, but no value.
6+
tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(31,1): error TS2693: 'I' only refers to a type, but is being used as a value here.
77

88

99
==== tests/cases/conformance/expressions/valuesAndReferences/assignments.ts (6 errors) ====
@@ -49,4 +49,4 @@ tests/cases/conformance/expressions/valuesAndReferences/assignments.ts(31,1): er
4949
interface I { }
5050
I = null; // Error
5151
~
52-
!!! error TS2692: Cannot find name 'I'. A type exists with this name, but no value.
52+
!!! error TS2693: 'I' only refers to a type, but is being used as a value here.

tests/baselines/reference/classExtendsInterfaceInExpression.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/classExtendsInterfaceInExpression.ts(7,25): error TS2692: Cannot find name 'A'. A type exists with this name, but no value.
1+
tests/cases/compiler/classExtendsInterfaceInExpression.ts(7,25): error TS2693: 'A' only refers to a type, but is being used as a value here.
22

33

44
==== tests/cases/compiler/classExtendsInterfaceInExpression.ts (1 errors) ====
@@ -10,5 +10,5 @@ tests/cases/compiler/classExtendsInterfaceInExpression.ts(7,25): error TS2692: C
1010

1111
class C extends factory(A) {}
1212
~
13-
!!! error TS2692: Cannot find name 'A'. A type exists with this name, but no value.
13+
!!! error TS2693: 'A' only refers to a type, but is being used as a value here.
1414

tests/baselines/reference/errorsOnImportedSymbol.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
tests/cases/compiler/errorsOnImportedSymbol_1.ts(2,13): error TS2692: Cannot find name 'Sammy'. A type exists with this name, but no value.
2-
tests/cases/compiler/errorsOnImportedSymbol_1.ts(3,9): error TS2692: Cannot find name 'Sammy'. A type exists with this name, but no value.
1+
tests/cases/compiler/errorsOnImportedSymbol_1.ts(2,13): error TS2693: 'Sammy' only refers to a type, but is being used as a value here.
2+
tests/cases/compiler/errorsOnImportedSymbol_1.ts(3,9): error TS2693: 'Sammy' only refers to a type, but is being used as a value here.
33

44

55
==== tests/cases/compiler/errorsOnImportedSymbol_1.ts (2 errors) ====
66
import Sammy = require("./errorsOnImportedSymbol_0");
77
var x = new Sammy.Sammy();
88
~~~~~
9-
!!! error TS2692: Cannot find name 'Sammy'. A type exists with this name, but no value.
9+
!!! error TS2693: 'Sammy' only refers to a type, but is being used as a value here.
1010
var y = Sammy.Sammy();
1111
~~~~~
12-
!!! error TS2692: Cannot find name 'Sammy'. A type exists with this name, but no value.
12+
!!! error TS2693: 'Sammy' only refers to a type, but is being used as a value here.
1313

1414

1515
==== tests/cases/compiler/errorsOnImportedSymbol_0.ts (0 errors) ====

tests/baselines/reference/es6ExportEqualsInterop.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/main.ts(15,1): error TS2692: Cannot find name 'z1'. A type exists with this name, but no value.
1+
tests/cases/compiler/main.ts(15,1): error TS2693: 'z1' only refers to a type, but is being used as a value here.
22
tests/cases/compiler/main.ts(21,4): error TS2339: Property 'a' does not exist on type '() => any'.
33
tests/cases/compiler/main.ts(23,4): error TS2339: Property 'a' does not exist on type 'typeof Foo'.
44
tests/cases/compiler/main.ts(27,8): error TS1192: Module '"interface"' has no default export.
@@ -49,7 +49,7 @@ tests/cases/compiler/main.ts(106,15): error TS2498: Module '"class-module"' uses
4949

5050
z1.a;
5151
~~
52-
!!! error TS2692: Cannot find name 'z1'. A type exists with this name, but no value.
52+
!!! error TS2693: 'z1' only refers to a type, but is being used as a value here.
5353
z2.a;
5454
z3.a;
5555
z4.a;

tests/baselines/reference/exportAssignmentOfDeclaredExternalModule.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
tests/cases/compiler/exportAssignmentOfDeclaredExternalModule_1.ts(3,13): error TS2692: Cannot find name 'Sammy'. A type exists with this name, but no value.
2-
tests/cases/compiler/exportAssignmentOfDeclaredExternalModule_1.ts(4,9): error TS2692: Cannot find name 'Sammy'. A type exists with this name, but no value.
1+
tests/cases/compiler/exportAssignmentOfDeclaredExternalModule_1.ts(3,13): error TS2693: 'Sammy' only refers to a type, but is being used as a value here.
2+
tests/cases/compiler/exportAssignmentOfDeclaredExternalModule_1.ts(4,9): error TS2693: 'Sammy' only refers to a type, but is being used as a value here.
33

44

55
==== tests/cases/compiler/exportAssignmentOfDeclaredExternalModule_1.ts (2 errors) ====
66
///<reference path='exportAssignmentOfDeclaredExternalModule_0.ts'/>
77
import Sammy = require('./exportAssignmentOfDeclaredExternalModule_0');
88
var x = new Sammy(); // error to use as constructor as there is not constructor symbol
99
~~~~~
10-
!!! error TS2692: Cannot find name 'Sammy'. A type exists with this name, but no value.
10+
!!! error TS2693: 'Sammy' only refers to a type, but is being used as a value here.
1111
var y = Sammy(); // error to use interface name as call target
1212
~~~~~
13-
!!! error TS2692: Cannot find name 'Sammy'. A type exists with this name, but no value.
13+
!!! error TS2693: 'Sammy' only refers to a type, but is being used as a value here.
1414
var z: Sammy; // no error - z is of type interface Sammy from module 'M'
1515
var a = new z(); // constructor - no error
1616
var b = z(); // call signature - no error
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/compiler/genericConstructInvocationWithNoTypeArg.ts(4,27): error TS2692: Cannot find name 'Foo'. A type exists with this name, but no value.
1+
tests/cases/compiler/genericConstructInvocationWithNoTypeArg.ts(4,27): error TS2693: 'Foo' only refers to a type, but is being used as a value here.
22

33

44
==== tests/cases/compiler/genericConstructInvocationWithNoTypeArg.ts (1 errors) ====
@@ -7,5 +7,5 @@ tests/cases/compiler/genericConstructInvocationWithNoTypeArg.ts(4,27): error TS2
77
}
88
var f2: Foo<number> = new Foo(3);
99
~~~
10-
!!! error TS2692: Cannot find name 'Foo'. A type exists with this name, but no value.
10+
!!! error TS2693: 'Foo' only refers to a type, but is being used as a value here.
1111

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
tests/cases/compiler/inheritFromGenericTypeParameter.ts(1,20): error TS2692: Cannot find name 'T'. A type exists with this name, but no value.
1+
tests/cases/compiler/inheritFromGenericTypeParameter.ts(1,20): error TS2693: 'T' only refers to a type, but is being used as a value here.
22
tests/cases/compiler/inheritFromGenericTypeParameter.ts(2,24): error TS2312: An interface may only extend a class or another interface.
33

44

55
==== tests/cases/compiler/inheritFromGenericTypeParameter.ts (2 errors) ====
66
class C<T> extends T { }
77
~
8-
!!! error TS2692: Cannot find name 'T'. A type exists with this name, but no value.
8+
!!! error TS2693: 'T' only refers to a type, but is being used as a value here.
99
interface I<T> extends T { }
1010
~
1111
!!! error TS2312: An interface may only extend a class or another interface.

0 commit comments

Comments
 (0)