Skip to content

Commit afe90f1

Browse files
Feed generalized source type to elaboration.
1 parent b38b0c7 commit afe90f1

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/compiler/checker.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15730,24 +15730,34 @@ namespace ts {
1573015730
function reportRelationError(message: DiagnosticMessage | undefined, source: Type, target: Type) {
1573115731
if (incompatibleStack.length) reportIncompatibleStack();
1573215732
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+
}
1573415744

1573515745
if (target.flags & TypeFlags.TypeParameter) {
1573615746
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)))) {
1573915749
reportError(
1574015750
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,
1574215752
targetType,
15743-
typeToString(constraint!),
15753+
typeToString(constraint),
1574415754
);
1574515755
}
1574615756
else {
1574715757
reportError(
1574815758
Diagnostics._0_could_be_instantiated_with_an_arbitrary_type_which_could_be_unrelated_to_1,
1574915759
targetType,
15750-
sourceType
15760+
generalizedSourceType
1575115761
);
1575215762
}
1575315763
}
@@ -15764,10 +15774,6 @@ namespace ts {
1576415774
}
1576515775
}
1576615776

15767-
if (isLiteralType(source) && !typeCouldHaveTopLevelSingletonTypes(target)) {
15768-
generalizedSourceType = getTypeNameForErrorDisplay(getBaseTypeOfLiteralType(source));
15769-
}
15770-
1577115777
reportError(message, generalizedSourceType, targetType);
1577215778
}
1577315779

0 commit comments

Comments
 (0)