@@ -15730,24 +15730,34 @@ namespace ts {
15730
15730
function reportRelationError(message: DiagnosticMessage | undefined, source: Type, target: Type) {
15731
15731
if (incompatibleStack.length) reportIncompatibleStack();
15732
15732
const [sourceType, targetType] = getTypeNamesForErrorDisplay(source, target);
15733
- let generalizedSourceType = sourceType;
15733
+ let generalizedSource: Type;
15734
+ let generalizedSourceType: string;
15735
+
15736
+ if (isLiteralType(source) && !typeCouldHaveTopLevelSingletonTypes(target)) {
15737
+ generalizedSource = getBaseTypeOfLiteralType(source);
15738
+ generalizedSourceType = getTypeNameForErrorDisplay(generalizedSource);
15739
+ }
15740
+ else {
15741
+ generalizedSource = source;
15742
+ generalizedSourceType = sourceType;
15743
+ }
15734
15744
15735
15745
if (target.flags & TypeFlags.TypeParameter) {
15736
15746
const constraint = getBaseConstraintOfType(target);
15737
- const constraintElab = constraint && isTypeAssignableTo(source, constraint) ;
15738
- if (constraintElab ) {
15747
+ let needsOriginalSource ;
15748
+ if (constraint && (isTypeAssignableTo(generalizedSource, constraint) || (needsOriginalSource = isTypeAssignableTo(source, constraint))) ) {
15739
15749
reportError(
15740
15750
Diagnostics._0_is_assignable_to_the_constraint_of_type_1_but_1_could_be_instantiated_with_a_different_subtype_of_constraint_2,
15741
- sourceType,
15751
+ needsOriginalSource ? sourceType : generalizedSourceType ,
15742
15752
targetType,
15743
- typeToString(constraint! ),
15753
+ typeToString(constraint),
15744
15754
);
15745
15755
}
15746
15756
else {
15747
15757
reportError(
15748
15758
Diagnostics._0_could_be_instantiated_with_an_arbitrary_type_which_could_be_unrelated_to_1,
15749
15759
targetType,
15750
- sourceType
15760
+ generalizedSourceType
15751
15761
);
15752
15762
}
15753
15763
}
@@ -15764,10 +15774,6 @@ namespace ts {
15764
15774
}
15765
15775
}
15766
15776
15767
- if (isLiteralType(source) && !typeCouldHaveTopLevelSingletonTypes(target)) {
15768
- generalizedSourceType = getTypeNameForErrorDisplay(getBaseTypeOfLiteralType(source));
15769
- }
15770
-
15771
15777
reportError(message, generalizedSourceType, targetType);
15772
15778
}
15773
15779
0 commit comments