Skip to content

Commit f7da7e9

Browse files
committed
More helpful error messaging when a type is used as a value
1 parent 10d1e02 commit f7da7e9

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/compiler/checker.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,8 @@ namespace ts {
879879
if (nameNotFoundMessage) {
880880
if (!errorLocation ||
881881
!checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg) &&
882-
!checkAndReportErrorForExtendingInterface(errorLocation)) {
882+
!checkAndReportErrorForExtendingInterface(errorLocation) &&
883+
!checkAndReportErrorForUsingTypeAsValue(errorLocation, name, meaning, nameNotFoundMessage, nameArg)) {
883884
error(errorLocation, nameNotFoundMessage, typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg));
884885
}
885886
}
@@ -989,6 +990,22 @@ namespace ts {
989990
}
990991
}
991992

993+
function checkAndReportErrorForUsingTypeAsValue(errorLocation: Node, name: string, meaning: SymbolFlags, nameNotFoundMessage: DiagnosticMessage, nameArg: string | Identifier): boolean {
994+
const strictlyValueMeanings = SymbolFlags.Value & ~SymbolFlags.Type;
995+
const strictlyTypeMeanings = SymbolFlags.Type & ~SymbolFlags.Value;
996+
997+
if (!(meaning & strictlyValueMeanings)) {
998+
return false;
999+
}
1000+
1001+
const nameAsType = resolveName(errorLocation, name, strictlyTypeMeanings, nameNotFoundMessage, nameArg);
1002+
if (nameAsType) {
1003+
error(errorLocation, Diagnostics.Cannot_find_name_0_A_type_exists_with_this_name_but_no_value, name);
1004+
return true;
1005+
}
1006+
1007+
return false;
1008+
}
9921009

9931010
function checkResolvedBlockScopedVariable(result: Symbol, errorLocation: Node): void {
9941011
Debug.assert((result.flags & SymbolFlags.BlockScopedVariable) !== 0);

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,6 +1951,10 @@
19511951
"category": "Error",
19521952
"code": 2690
19531953
},
1954+
"Cannot find name '{0}'. A type exists with this name, but no value.": {
1955+
"category": "Error",
1956+
"code": 2691
1957+
},
19541958
"Import declaration '{0}' is using private name '{1}'.": {
19551959
"category": "Error",
19561960
"code": 4000
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
interface Interface {
2+
3+
}
4+
5+
class Class {
6+
7+
}
8+
9+
type typeAliasForClass = Class;
10+
type typeAliasForNumber = number;
11+
type objectType = { x: number };
12+
13+
function func(a: number) {
14+
15+
}
16+
17+
let one = Interface;
18+
let two = typeAliasForClass;
19+
let three = typeAliasForNumber;
20+
let four = objectType;
21+
func(typeAliasForNumber);

0 commit comments

Comments
 (0)