Skip to content

Commit 248cbba

Browse files
committed
Delete the {} and unconstrained type parameter assignability rule
1 parent bf992a5 commit 248cbba

File tree

76 files changed

+1379
-559
lines changed

Some content is hidden

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

76 files changed

+1379
-559
lines changed

src/compiler/checker.ts

+8-13
Original file line numberDiff line numberDiff line change
@@ -13678,22 +13678,17 @@ namespace ts {
1367813678
}
1367913679
else {
1368013680
const constraint = getConstraintOfType(<TypeVariable>source);
13681-
if (!constraint || (source.flags & TypeFlags.TypeParameter && constraint.flags & TypeFlags.Any)) {
13682-
// A type variable with no constraint is not related to the non-primitive object type.
13683-
if (result = isRelatedTo(emptyObjectType, extractTypesOfKind(target, ~TypeFlags.NonPrimitive))) {
13681+
if (constraint) {
13682+
// hi-speed no-this-instantiation check (less accurate, but avoids costly `this`-instantiation when the constraint will suffice), see #28231 for report on why this is needed
13683+
if (result = isRelatedTo(constraint, target, /*reportErrors*/ false, /*headMessage*/ undefined, isIntersectionConstituent)) {
13684+
resetErrorInfo(saveErrorInfo);
13685+
return result;
13686+
}
13687+
// slower, fuller, this-instantiated check (necessary when comparing raw `this` types from base classes), see `subclassWithPolymorphicThisIsAssignable.ts` test for example
13688+
else if (result = isRelatedTo(getTypeWithThisArgument(constraint, source), target, reportErrors, /*headMessage*/ undefined, isIntersectionConstituent)) {
1368413689
resetErrorInfo(saveErrorInfo);
1368513690
return result;
1368613691
}
13687-
}
13688-
// hi-speed no-this-instantiation check (less accurate, but avoids costly `this`-instantiation when the constraint will suffice), see #28231 for report on why this is needed
13689-
else if (result = isRelatedTo(constraint, target, /*reportErrors*/ false, /*headMessage*/ undefined, isIntersectionConstituent)) {
13690-
resetErrorInfo(saveErrorInfo);
13691-
return result;
13692-
}
13693-
// slower, fuller, this-instantiated check (necessary when comparing raw `this` types from base classes), see `subclassWithPolymorphicThisIsAssignable.ts` test for example
13694-
else if (result = isRelatedTo(getTypeWithThisArgument(constraint, source), target, reportErrors, /*headMessage*/ undefined, isIntersectionConstituent)) {
13695-
resetErrorInfo(saveErrorInfo);
13696-
return result;
1369713692
}
1369813693
}
1369913694
}

tests/baselines/reference/assignmentCompatWithCallSignatures5.errors.txt

-4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
55
Type '{ foo: U; bar: U; }' is not assignable to type '{ foo: T; bar: T; }'.
66
Types of property 'foo' are incompatible.
77
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 '{}'.
98
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[]'.
109
Types of parameters 'x' and 'x' are incompatible.
1110
Type '{ a: U; b: V; }' is not assignable to type '{ a: U; b: U; }'.
1211
Types of property 'b' are incompatible.
1312
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 '{}'.
1513
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[]'.
1614
Types of parameters 'x' and 'x' are incompatible.
1715
Type '{ a: U; b: V; }' is not assignable to type '{ a: Base; b: Base; }'.
@@ -81,7 +79,6 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
8179
!!! error TS2322: Type '{ foo: U; bar: U; }' is not assignable to type '{ foo: T; bar: T; }'.
8280
!!! error TS2322: Types of property 'foo' are incompatible.
8381
!!! 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 '{}'.
8582
var b15: <U, V>(x: { a: U; b: V; }) => U[];
8683
a15 = b15; // ok, T = U, T = V
8784
b15 = a15; // ok
@@ -91,7 +88,6 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
9188
!!! error TS2322: Type '{ a: U; b: V; }' is not assignable to type '{ a: U; b: U; }'.
9289
!!! error TS2322: Types of property 'b' are incompatible.
9390
!!! 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 '{}'.
9591
var b16: <T>(x: { a: T; b: T }) => T[];
9692
a15 = b16; // ok
9793
b15 = a16; // ok

tests/baselines/reference/assignmentCompatWithCallSignatures6.errors.txt

-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
55
Type '{ foo: U; bar: U; }' is not assignable to type '{ foo: T; bar: T; }'.
66
Types of property 'foo' are incompatible.
77
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 '{}'.
98
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[]'.
109
Types of parameters 'x' and 'x' are incompatible.
1110
Type '{ a: T; b: T; }' is not assignable to type '{ a: Base; b: Base; }'.
@@ -62,7 +61,6 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
6261
!!! error TS2322: Type '{ foo: U; bar: U; }' is not assignable to type '{ foo: T; bar: T; }'.
6362
!!! error TS2322: Types of property 'foo' are incompatible.
6463
!!! 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 '{}'.
6664
var b16: <T>(x: { a: T; b: T }) => T[];
6765
x.a16 = b16;
6866
b16 = x.a16;

tests/baselines/reference/assignmentCompatWithConstructSignatures5.errors.txt

-4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
55
Type '{ foo: U; bar: U; }' is not assignable to type '{ foo: T; bar: T; }'.
66
Types of property 'foo' are incompatible.
77
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 '{}'.
98
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithConstructSignatures5.ts(55,1): error TS2322: Type 'new <T>(x: { a: T; b: T; }) => T[]' is not assignable to type 'new <U, V>(x: { a: U; b: V; }) => U[]'.
109
Types of parameters 'x' and 'x' are incompatible.
1110
Type '{ a: U; b: V; }' is not assignable to type '{ a: U; b: U; }'.
1211
Types of property 'b' are incompatible.
1312
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 '{}'.
1513
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithConstructSignatures5.ts(58,1): error TS2322: Type 'new <T extends Base>(x: { a: T; b: T; }) => T[]' is not assignable to type 'new <U, V>(x: { a: U; b: V; }) => U[]'.
1614
Types of parameters 'x' and 'x' are incompatible.
1715
Type '{ a: U; b: V; }' is not assignable to type '{ a: Base; b: Base; }'.
@@ -81,7 +79,6 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
8179
!!! error TS2322: Type '{ foo: U; bar: U; }' is not assignable to type '{ foo: T; bar: T; }'.
8280
!!! error TS2322: Types of property 'foo' are incompatible.
8381
!!! 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 '{}'.
8582
var b15: new <U, V>(x: { a: U; b: V; }) => U[];
8683
a15 = b15; // ok
8784
b15 = a15; // ok
@@ -91,7 +88,6 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
9188
!!! error TS2322: Type '{ a: U; b: V; }' is not assignable to type '{ a: U; b: U; }'.
9289
!!! error TS2322: Types of property 'b' are incompatible.
9390
!!! 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 '{}'.
9591
var b16: new <T>(x: { a: T; b: T }) => T[];
9692
a15 = b16; // ok
9793
b15 = a16; // ok

tests/baselines/reference/assignmentCompatWithConstructSignatures6.errors.txt

-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
55
Type '{ foo: U; bar: U; }' is not assignable to type '{ foo: T; bar: T; }'.
66
Types of property 'foo' are incompatible.
77
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 '{}'.
98
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithConstructSignatures6.ts(42,1): error TS2322: Type 'new <T extends Base>(x: { a: T; b: T; }) => T[]' is not assignable to type 'new <T>(x: { a: T; b: T; }) => T[]'.
109
Types of parameters 'x' and 'x' are incompatible.
1110
Type '{ a: T; b: T; }' is not assignable to type '{ a: Base; b: Base; }'.
@@ -62,7 +61,6 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
6261
!!! error TS2322: Type '{ foo: U; bar: U; }' is not assignable to type '{ foo: T; bar: T; }'.
6362
!!! error TS2322: Types of property 'foo' are incompatible.
6463
!!! 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 '{}'.
6664
var b16: new <T>(x: { a: T; b: T }) => T[];
6765
x.a16 = b16;
6866
b16 = x.a16;

0 commit comments

Comments
 (0)