Skip to content

Commit dcc7394

Browse files
Fix 33436 (#35225)
* Fix 33436 * Fix code * Fix error message after bad merge * Remove whitespace
1 parent d727d5a commit dcc7394

File tree

96 files changed

+661
-520
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+661
-520
lines changed

src/compiler/checker.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15218,13 +15218,24 @@ namespace ts {
1521815218
if (incompatibleStack.length) reportIncompatibleStack();
1521915219
const [sourceType, targetType] = getTypeNamesForErrorDisplay(source, target);
1522015220

15221-
if (target.flags & TypeFlags.TypeParameter && target.immediateBaseConstraint !== undefined && isTypeAssignableTo(source, target.immediateBaseConstraint)) {
15222-
reportError(
15223-
Diagnostics._0_is_assignable_to_the_constraint_of_type_1_but_1_could_be_instantiated_with_a_different_subtype_of_constraint_2,
15224-
sourceType,
15225-
targetType,
15226-
typeToString(target.immediateBaseConstraint),
15227-
);
15221+
if (target.flags & TypeFlags.TypeParameter) {
15222+
const constraint = getBaseConstraintOfType(target);
15223+
const constraintElab = constraint && isTypeAssignableTo(source, constraint);
15224+
if (constraintElab) {
15225+
reportError(
15226+
Diagnostics._0_is_assignable_to_the_constraint_of_type_1_but_1_could_be_instantiated_with_a_different_subtype_of_constraint_2,
15227+
sourceType,
15228+
targetType,
15229+
typeToString(constraint!),
15230+
);
15231+
}
15232+
else {
15233+
reportError(
15234+
Diagnostics._0_could_be_instantiated_with_an_arbitrary_type_which_could_be_unrelated_to_1,
15235+
targetType,
15236+
sourceType
15237+
);
15238+
}
1522815239
}
1522915240

1523015241
if (!message) {

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3445,6 +3445,10 @@
34453445
"category": "Error",
34463446
"code": 5081
34473447
},
3448+
"'{0}' could be instantiated with an arbitrary type which could be unrelated to '{1}'.": {
3449+
"category": "Error",
3450+
"code": 5082
3451+
},
34483452

34493453
"Generates a sourcemap for each corresponding '.d.ts' file.": {
34503454
"category": "Message",

tests/baselines/reference/assignmentCompatWithCallSignatures3.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
1414
Types of parameters 'x' and 'x' are incompatible.
1515
Types of parameters 'arg' and 'arg' are incompatible.
1616
Type 'string' is not assignable to type 'T'.
17-
'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
17+
'T' could be instantiated with an arbitrary type which could be unrelated to 'string'.
1818
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures3.ts(62,1): error TS2322: Type '(x: (arg: Base) => Derived) => Base' is not assignable to type '<T extends Base, U extends Derived>(x: (arg: T) => U) => T'.
1919
Types of parameters 'x' and 'x' are incompatible.
2020
Types of parameters 'arg' and 'arg' are incompatible.
@@ -143,7 +143,7 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
143143
!!! error TS2322: Types of parameters 'x' and 'x' are incompatible.
144144
!!! error TS2322: Types of parameters 'arg' and 'arg' are incompatible.
145145
!!! error TS2322: Type 'string' is not assignable to type 'T'.
146-
!!! error TS2322: 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
146+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'string'.
147147
var b6: <T extends Base, U extends Derived>(x: (arg: T) => U) => T;
148148
a6 = b6; // ok
149149
b6 = a6; // ok

tests/baselines/reference/assignmentCompatWithCallSignatures4.errors.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
1919
'Base' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'Base'.
2020
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures4.ts(58,9): error TS2322: Type '(...x: Base[]) => Base' is not assignable to type '<T extends Derived>(...x: T[]) => T'.
2121
Type 'Base' is not assignable to type 'T'.
22+
'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
2223
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures4.ts(62,9): error TS2322: Type '(x: { foo: string; }, y: { foo: string; bar: string; }) => Base' is not assignable to type '<T extends Derived>(x: T, y: T) => T'.
2324
Type 'Base' is not assignable to type 'T'.
25+
'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
2426
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures4.ts(66,9): error TS2322: Type '(x: Base[], y: Derived2[]) => Derived[]' is not assignable to type '<T extends Derived2[]>(x: Base[], y: Base[]) => T'.
2527
Type 'Derived[]' is not assignable to type 'T'.
28+
'T' could be instantiated with an arbitrary type which could be unrelated to 'Derived[]'.
2629
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures4.ts(69,9): error TS2322: Type '<T>(x: { a: T; b: T; }) => T' is not assignable to type '(x: { a: string; b: number; }) => number'.
2730
Types of parameters 'x' and 'x' are incompatible.
2831
Type '{ a: string; b: number; }' is not assignable to type '{ a: string; b: string; }'.
@@ -47,7 +50,7 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
4750
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures4.ts(89,9): error TS2322: Type '<T>(x: T) => string[]' is not assignable to type '<T>(x: T) => T[]'.
4851
Type 'string[]' is not assignable to type 'T[]'.
4952
Type 'string' is not assignable to type 'T'.
50-
'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
53+
'T' could be instantiated with an arbitrary type which could be unrelated to 'string'.
5154
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures4.ts(90,9): error TS2322: Type '<T>(x: T) => T[]' is not assignable to type '<T>(x: T) => string[]'.
5255
Type 'T[]' is not assignable to type 'string[]'.
5356
Type 'T' is not assignable to type 'string'.
@@ -57,7 +60,7 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
5760
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures4.ts(96,9): error TS2322: Type '<T>(x: T) => string[]' is not assignable to type '<T>(x: T) => T[]'.
5861
Type 'string[]' is not assignable to type 'T[]'.
5962
Type 'string' is not assignable to type 'T'.
60-
'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
63+
'T' could be instantiated with an arbitrary type which could be unrelated to 'string'.
6164

6265

6366
==== tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures4.ts (15 errors) ====
@@ -145,20 +148,23 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
145148
~~~
146149
!!! error TS2322: Type '(...x: Base[]) => Base' is not assignable to type '<T extends Derived>(...x: T[]) => T'.
147150
!!! error TS2322: Type 'Base' is not assignable to type 'T'.
151+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
148152

149153
var b11: <T extends Derived>(x: T, y: T) => T;
150154
a11 = b11;
151155
b11 = a11;
152156
~~~
153157
!!! error TS2322: Type '(x: { foo: string; }, y: { foo: string; bar: string; }) => Base' is not assignable to type '<T extends Derived>(x: T, y: T) => T'.
154158
!!! error TS2322: Type 'Base' is not assignable to type 'T'.
159+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
155160

156161
var b12: <T extends Array<Derived2>>(x: Array<Base>, y: Array<Base>) => T;
157162
a12 = b12;
158163
b12 = a12;
159164
~~~
160165
!!! error TS2322: Type '(x: Base[], y: Derived2[]) => Derived[]' is not assignable to type '<T extends Derived2[]>(x: Base[], y: Base[]) => T'.
161166
!!! error TS2322: Type 'Derived[]' is not assignable to type 'T'.
167+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Derived[]'.
162168

163169
var b15: <T>(x: { a: T; b: T }) => T;
164170
a15 = b15;
@@ -211,7 +217,7 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
211217
!!! error TS2322: Type '<T>(x: T) => string[]' is not assignable to type '<T>(x: T) => T[]'.
212218
!!! error TS2322: Type 'string[]' is not assignable to type 'T[]'.
213219
!!! error TS2322: Type 'string' is not assignable to type 'T'.
214-
!!! error TS2322: 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
220+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'string'.
215221
b2 = a2;
216222
~~
217223
!!! error TS2322: Type '<T>(x: T) => T[]' is not assignable to type '<T>(x: T) => string[]'.
@@ -231,6 +237,6 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
231237
!!! error TS2322: Type '<T>(x: T) => string[]' is not assignable to type '<T>(x: T) => T[]'.
232238
!!! error TS2322: Type 'string[]' is not assignable to type 'T[]'.
233239
!!! error TS2322: Type 'string' is not assignable to type 'T'.
234-
!!! error TS2322: 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
240+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'string'.
235241
}
236242
}

tests/baselines/reference/assignmentCompatWithCallSignatures5.errors.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures5.ts(40,1): error TS2322: Type '<T>(x: T) => void' is not assignable to type '<T>(x: T) => T'.
22
Type 'void' is not assignable to type 'T'.
3+
'T' could be instantiated with an arbitrary type which could be unrelated to 'void'.
34
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures5.ts(52,1): error TS2322: Type '<T>(x: { foo: T; }, y: { foo: T; bar: T; }) => Base' is not assignable to type '<T, U>(x: { foo: T; }, y: { foo: U; bar: U; }) => Base'.
45
Types of parameters 'y' and 'y' are incompatible.
56
Type '{ foo: U; bar: U; }' is not assignable to type '{ foo: T; bar: T; }'.
67
Types of property 'foo' are incompatible.
78
Type 'U' is not assignable to type 'T'.
8-
'U' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
9+
'T' could be instantiated with an arbitrary type which could be unrelated to 'U'.
910
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures5.ts(55,1): error TS2322: Type '<T>(x: { a: T; b: T; }) => T[]' is not assignable to type '<U, V>(x: { a: U; b: V; }) => U[]'.
1011
Types of parameters 'x' and 'x' are incompatible.
1112
Type '{ a: U; b: V; }' is not assignable to type '{ a: U; b: U; }'.
1213
Types of property 'b' are incompatible.
1314
Type 'V' is not assignable to type 'U'.
14-
'V' is assignable to the constraint of type 'U', but 'U' could be instantiated with a different subtype of constraint '{}'.
15+
'U' could be instantiated with an arbitrary type which could be unrelated to 'V'.
1516
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures5.ts(58,1): error TS2322: Type '<T extends Base>(x: { a: T; b: T; }) => T[]' is not assignable to type '<U, V>(x: { a: U; b: V; }) => U[]'.
1617
Types of parameters 'x' and 'x' are incompatible.
1718
Type '{ a: U; b: V; }' is not assignable to type '{ a: Base; b: Base; }'.
@@ -63,6 +64,7 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
6364
~~
6465
!!! error TS2322: Type '<T>(x: T) => void' is not assignable to type '<T>(x: T) => T'.
6566
!!! error TS2322: Type 'void' is not assignable to type 'T'.
67+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'void'.
6668
var b4: <T, U>(x: T, y: U) => string;
6769
a4 = b4; // ok
6870
b4 = a4; // ok
@@ -81,7 +83,7 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
8183
!!! error TS2322: Type '{ foo: U; bar: U; }' is not assignable to type '{ foo: T; bar: T; }'.
8284
!!! error TS2322: Types of property 'foo' are incompatible.
8385
!!! error TS2322: Type 'U' is not assignable to type 'T'.
84-
!!! error TS2322: 'U' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
86+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'U'.
8587
var b15: <U, V>(x: { a: U; b: V; }) => U[];
8688
a15 = b15; // ok, T = U, T = V
8789
b15 = a15; // ok
@@ -91,7 +93,7 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
9193
!!! error TS2322: Type '{ a: U; b: V; }' is not assignable to type '{ a: U; b: U; }'.
9294
!!! error TS2322: Types of property 'b' are incompatible.
9395
!!! error TS2322: Type 'V' is not assignable to type 'U'.
94-
!!! error TS2322: 'V' is assignable to the constraint of type 'U', but 'U' could be instantiated with a different subtype of constraint '{}'.
96+
!!! error TS2322: 'U' could be instantiated with an arbitrary type which could be unrelated to 'V'.
9597
var b16: <T>(x: { a: T; b: T }) => T[];
9698
a15 = b16; // ok
9799
b15 = a16; // ok

tests/baselines/reference/assignmentCompatWithCallSignatures6.errors.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures6.ts(30,1): error TS2322: Type '<T>(x: T) => void' is not assignable to type '<T>(x: T) => T'.
22
Type 'void' is not assignable to type 'T'.
3+
'T' could be instantiated with an arbitrary type which could be unrelated to 'void'.
34
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures6.ts(39,1): error TS2322: Type '<T>(x: { foo: T; }, y: { foo: T; bar: T; }) => Base' is not assignable to type '<T, U>(x: { foo: T; }, y: { foo: U; bar: U; }) => Base'.
45
Types of parameters 'y' and 'y' are incompatible.
56
Type '{ foo: U; bar: U; }' is not assignable to type '{ foo: T; bar: T; }'.
67
Types of property 'foo' are incompatible.
78
Type 'U' is not assignable to type 'T'.
8-
'U' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
9+
'T' could be instantiated with an arbitrary type which could be unrelated to 'U'.
910
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithCallSignatures6.ts(42,1): error TS2322: Type '<T extends Base>(x: { a: T; b: T; }) => T[]' is not assignable to type '<T>(x: { a: T; b: T; }) => T[]'.
1011
Types of parameters 'x' and 'x' are incompatible.
1112
Type '{ a: T; b: T; }' is not assignable to type '{ a: Base; b: Base; }'.
@@ -47,6 +48,7 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
4748
~~
4849
!!! error TS2322: Type '<T>(x: T) => void' is not assignable to type '<T>(x: T) => T'.
4950
!!! error TS2322: Type 'void' is not assignable to type 'T'.
51+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'void'.
5052
var b4: <T, U>(x: T, y: U) => string;
5153
x.a4 = b4;
5254
b4 = x.a4;
@@ -62,7 +64,7 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
6264
!!! error TS2322: Type '{ foo: U; bar: U; }' is not assignable to type '{ foo: T; bar: T; }'.
6365
!!! error TS2322: Types of property 'foo' are incompatible.
6466
!!! error TS2322: Type 'U' is not assignable to type 'T'.
65-
!!! error TS2322: 'U' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
67+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'U'.
6668
var b16: <T>(x: { a: T; b: T }) => T[];
6769
x.a16 = b16;
6870
b16 = x.a16;

tests/baselines/reference/assignmentCompatWithConstructSignatures3.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
1414
Types of parameters 'x' and 'x' are incompatible.
1515
Types of parameters 'arg' and 'arg' are incompatible.
1616
Type 'string' is not assignable to type 'T'.
17-
'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
17+
'T' could be instantiated with an arbitrary type which could be unrelated to 'string'.
1818
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithConstructSignatures3.ts(62,1): error TS2322: Type 'new (x: (arg: Base) => Derived) => Base' is not assignable to type 'new <T extends Base, U extends Derived>(x: (arg: T) => U) => T'.
1919
Types of parameters 'x' and 'x' are incompatible.
2020
Types of parameters 'arg' and 'arg' are incompatible.
@@ -143,7 +143,7 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
143143
!!! error TS2322: Types of parameters 'x' and 'x' are incompatible.
144144
!!! error TS2322: Types of parameters 'arg' and 'arg' are incompatible.
145145
!!! error TS2322: Type 'string' is not assignable to type 'T'.
146-
!!! error TS2322: 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{}'.
146+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'string'.
147147
var b6: new <T extends Base, U extends Derived>(x: (arg: T) => U) => T;
148148
a6 = b6; // ok
149149
b6 = a6; // ok

0 commit comments

Comments
 (0)