More helpful error messaging when a type is used as a value

This commit is contained in:
Justin Bay
2016-08-14 19:27:33 -04:00
parent 10d1e02916
commit f7da7e9006
3 changed files with 43 additions and 1 deletions

View File

@@ -879,7 +879,8 @@ namespace ts {
if (nameNotFoundMessage) {
if (!errorLocation ||
!checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg) &&
!checkAndReportErrorForExtendingInterface(errorLocation)) {
!checkAndReportErrorForExtendingInterface(errorLocation) &&
!checkAndReportErrorForUsingTypeAsValue(errorLocation, name, meaning, nameNotFoundMessage, nameArg)) {
error(errorLocation, nameNotFoundMessage, typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg));
}
}
@@ -989,6 +990,22 @@ namespace ts {
}
}
function checkAndReportErrorForUsingTypeAsValue(errorLocation: Node, name: string, meaning: SymbolFlags, nameNotFoundMessage: DiagnosticMessage, nameArg: string | Identifier): boolean {
const strictlyValueMeanings = SymbolFlags.Value & ~SymbolFlags.Type;
const strictlyTypeMeanings = SymbolFlags.Type & ~SymbolFlags.Value;
if (!(meaning & strictlyValueMeanings)) {
return false;
}
const nameAsType = resolveName(errorLocation, name, strictlyTypeMeanings, nameNotFoundMessage, nameArg);
if (nameAsType) {
error(errorLocation, Diagnostics.Cannot_find_name_0_A_type_exists_with_this_name_but_no_value, name);
return true;
}
return false;
}
function checkResolvedBlockScopedVariable(result: Symbol, errorLocation: Node): void {
Debug.assert((result.flags & SymbolFlags.BlockScopedVariable) !== 0);

View File

@@ -1951,6 +1951,10 @@
"category": "Error",
"code": 2690
},
"Cannot find name '{0}'. A type exists with this name, but no value.": {
"category": "Error",
"code": 2691
},
"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
"code": 4000

View File

@@ -0,0 +1,21 @@
interface Interface {
}
class Class {
}
type typeAliasForClass = Class;
type typeAliasForNumber = number;
type objectType = { x: number };
function func(a: number) {
}
let one = Interface;
let two = typeAliasForClass;
let three = typeAliasForNumber;
let four = objectType;
func(typeAliasForNumber);