mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-15 12:51:30 -05:00
improve error message when type have same name (#27065)
* improve error message when type have same name * fix lint and function name * update test case * accept baseline
This commit is contained in:
committed by
Ryan Cavanaugh
parent
a3c852db9b
commit
bd178746de
@@ -3322,6 +3322,16 @@ namespace ts {
|
||||
return result;
|
||||
}
|
||||
|
||||
function getTypeNamesForErrorDisplay(left: Type, right: Type): [string, string] {
|
||||
let leftStr = typeToString(left);
|
||||
let rightStr = typeToString(right);
|
||||
if (leftStr === rightStr) {
|
||||
leftStr = typeToString(left, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType);
|
||||
rightStr = typeToString(right, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType);
|
||||
}
|
||||
return [leftStr, rightStr];
|
||||
}
|
||||
|
||||
function toNodeBuilderFlags(flags = TypeFormatFlags.None): NodeBuilderFlags {
|
||||
return flags & TypeFormatFlags.NodeBuilderFlagsMask;
|
||||
}
|
||||
@@ -12253,12 +12263,7 @@ namespace ts {
|
||||
}
|
||||
|
||||
function reportRelationError(message: DiagnosticMessage | undefined, source: Type, target: Type) {
|
||||
let sourceType = typeToString(source);
|
||||
let targetType = typeToString(target);
|
||||
if (sourceType === targetType) {
|
||||
sourceType = typeToString(source, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType);
|
||||
targetType = typeToString(target, /*enclosingDeclaration*/ undefined, TypeFormatFlags.UseFullyQualifiedType);
|
||||
}
|
||||
const [sourceType, targetType] = getTypeNamesForErrorDisplay(source, target);
|
||||
|
||||
if (!message) {
|
||||
if (relation === comparableRelation) {
|
||||
@@ -13209,7 +13214,7 @@ namespace ts {
|
||||
}
|
||||
if (props.length === 1) {
|
||||
const propName = symbolToString(unmatchedProperty);
|
||||
reportError(Diagnostics.Property_0_is_missing_in_type_1_but_required_in_type_2, propName, typeToString(source), typeToString(target));
|
||||
reportError(Diagnostics.Property_0_is_missing_in_type_1_but_required_in_type_2, propName, ...getTypeNamesForErrorDisplay(source, target));
|
||||
if (length(unmatchedProperty.declarations)) {
|
||||
associateRelatedInfo(createDiagnosticForNode(unmatchedProperty.declarations[0], Diagnostics._0_is_declared_here, propName));
|
||||
}
|
||||
@@ -23452,8 +23457,7 @@ namespace ts {
|
||||
}
|
||||
|
||||
function reportOperatorError() {
|
||||
const leftStr = typeToString(leftType);
|
||||
const rightStr = typeToString(rightType);
|
||||
const [leftStr, rightStr] = getTypeNamesForErrorDisplay(leftType, rightType);
|
||||
const errNode = errorNode || operatorToken;
|
||||
if (!tryGiveBetterPrimaryError(errNode, leftStr, rightStr)) {
|
||||
error(
|
||||
|
||||
Reference in New Issue
Block a user