mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-23 07:07:09 -05:00
More helpful error messaging when a type is used as a value
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
21
tests/cases/compiler/typeUsedAsValueError.ts
Normal file
21
tests/cases/compiler/typeUsedAsValueError.ts
Normal 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);
|
||||
Reference in New Issue
Block a user