Added custom error message when trying to assign constraint type to generic type parameter (#30394)

* Added custom error message when trying to assign constraint type to generic type parameter

Fixes #29049.

This also adds the new message in chained error messages. `typeParameterDiamond4.errors.txt` shows it appearing twice in the "diamond" scenario. I can't tell if this severely increased amount of nested messages is good or bad...?

* Updated diagnostic message per suggestion

* Align formatting with local custom
This commit is contained in:
Josh Goldberg
2019-04-30 11:35:23 -04:00
committed by Ryan Cavanaugh
parent 7016d45447
commit 6e736c120e
102 changed files with 919 additions and 156 deletions

View File

@@ -12328,6 +12328,15 @@ namespace ts {
function reportRelationError(message: DiagnosticMessage | undefined, source: Type, target: Type) {
const [sourceType, targetType] = getTypeNamesForErrorDisplay(source, target);
if (target.flags & TypeFlags.TypeParameter && target.immediateBaseConstraint !== undefined && isTypeAssignableTo(source, target.immediateBaseConstraint)) {
reportError(
Diagnostics._0_is_assignable_to_the_constraint_of_type_1_but_1_could_be_instantiated_with_a_different_subtype_of_constraint_2,
sourceType,
targetType,
typeToString(target.immediateBaseConstraint),
);
}
if (!message) {
if (relation === comparableRelation) {
message = Diagnostics.Type_0_is_not_comparable_to_type_1;

View File

@@ -3096,6 +3096,10 @@
"category": "Error",
"code": 5074
},
"'{0}' is assignable to the constraint of type '{1}', but '{1}' could be instantiated with a different subtype of constraint '{2}'.": {
"category": "Error",
"code": 5075
},
"Generates a sourcemap for each corresponding '.d.ts' file.": {
"category": "Message",