mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-15 03:09:59 -05:00
25840 - Add a more meaningful error message to the case when calling a public static method on an instance (#25922)
* add a more meaningful error message to the case when calling a public static method on an instance * Fix tests
This commit is contained in:
committed by
Nathan Shively-Sanders
parent
d8cbe34a05
commit
37b9a6bca4
@@ -3322,7 +3322,6 @@ namespace ts {
|
||||
// Anonymous types without a symbol are never circular.
|
||||
return createTypeNodeFromObjectType(type);
|
||||
}
|
||||
|
||||
function shouldWriteTypeOfFunctionSymbol() {
|
||||
const isStaticMethodSymbol = !!(symbol.flags & SymbolFlags.Method) && // typeof static method
|
||||
some(symbol.declarations, declaration => hasModifier(declaration, ModifierFlags.Static));
|
||||
@@ -9158,7 +9157,10 @@ namespace ts {
|
||||
}
|
||||
if (accessExpression && !isConstEnumObjectType(objectType)) {
|
||||
if (noImplicitAny && !compilerOptions.suppressImplicitAnyIndexErrors) {
|
||||
if (getIndexTypeOfType(objectType, IndexKind.Number)) {
|
||||
if (propName !== undefined && typeHasStaticProperty(propName, objectType)) {
|
||||
error(accessExpression, Diagnostics.Property_0_is_a_static_member_of_type_1, propName as string, typeToString(objectType));
|
||||
}
|
||||
else if (getIndexTypeOfType(objectType, IndexKind.Number)) {
|
||||
error(accessExpression.argumentExpression, Diagnostics.Element_implicitly_has_an_any_type_because_index_expression_is_not_of_type_number);
|
||||
}
|
||||
else {
|
||||
@@ -18055,22 +18057,26 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
}
|
||||
const promisedType = getPromisedTypeOfPromise(containingType);
|
||||
if (promisedType && getPropertyOfType(promisedType, propNode.escapedText)) {
|
||||
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_forget_to_use_await, declarationNameToString(propNode), typeToString(containingType));
|
||||
if (typeHasStaticProperty(propNode.escapedText, containingType)) {
|
||||
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_is_a_static_member_of_type_1, declarationNameToString(propNode), typeToString(containingType));
|
||||
}
|
||||
else {
|
||||
const suggestion = getSuggestedSymbolForNonexistentProperty(propNode, containingType);
|
||||
if (suggestion !== undefined) {
|
||||
const suggestedName = symbolName(suggestion);
|
||||
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2, declarationNameToString(propNode), typeToString(containingType), suggestedName);
|
||||
relatedInfo = suggestion.valueDeclaration && createDiagnosticForNode(suggestion.valueDeclaration, Diagnostics._0_is_declared_here, suggestedName);
|
||||
const promisedType = getPromisedTypeOfPromise(containingType);
|
||||
if (promisedType && getPropertyOfType(promisedType, propNode.escapedText)) {
|
||||
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_forget_to_use_await, declarationNameToString(propNode), typeToString(containingType));
|
||||
}
|
||||
else {
|
||||
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1, declarationNameToString(propNode), typeToString(containingType));
|
||||
const suggestion = getSuggestedSymbolForNonexistentProperty(propNode, containingType);
|
||||
if (suggestion !== undefined) {
|
||||
const suggestedName = symbolName(suggestion);
|
||||
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1_Did_you_mean_2, declarationNameToString(propNode), typeToString(containingType), suggestedName);
|
||||
relatedInfo = suggestion.valueDeclaration && createDiagnosticForNode(suggestion.valueDeclaration, Diagnostics._0_is_declared_here, suggestedName);
|
||||
}
|
||||
else {
|
||||
errorInfo = chainDiagnosticMessages(errorInfo, Diagnostics.Property_0_does_not_exist_on_type_1, declarationNameToString(propNode), typeToString(containingType));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const resultDiagnostic = createDiagnosticForNodeFromMessageChain(propNode, errorInfo);
|
||||
if (relatedInfo) {
|
||||
addRelatedInfo(resultDiagnostic, relatedInfo);
|
||||
@@ -18078,6 +18084,11 @@ namespace ts {
|
||||
diagnostics.add(resultDiagnostic);
|
||||
}
|
||||
|
||||
function typeHasStaticProperty(propName: __String, containingType: Type): boolean {
|
||||
const prop = containingType.symbol && getPropertyOfType(getTypeOfSymbol(containingType.symbol), propName);
|
||||
return prop !== undefined && prop.valueDeclaration && hasModifier(prop.valueDeclaration, ModifierFlags.Static);
|
||||
}
|
||||
|
||||
function getSuggestedSymbolForNonexistentProperty(name: Identifier | string, containingType: Type): Symbol | undefined {
|
||||
return getSpellingSuggestionForName(isString(name) ? name : idText(name), getPropertiesOfType(containingType), SymbolFlags.Value);
|
||||
}
|
||||
|
||||
@@ -2064,6 +2064,10 @@
|
||||
"category": "Error",
|
||||
"code": 2575
|
||||
},
|
||||
"Property '{0}' is a static member of type '{1}'": {
|
||||
"category": "Error",
|
||||
"code": 2576
|
||||
},
|
||||
"JSX element attributes type '{0}' may not be a union type.": {
|
||||
"category": "Error",
|
||||
"code": 2600
|
||||
|
||||
Reference in New Issue
Block a user