Skip to content

Commit 3b8b207

Browse files
Temporarily revert unconstrained type parameter strictness in TS 4.7 (#48923)
* Revert a change around allowing unconstrained type parameters to '{}'. * Accepted baselines.
1 parent bb887ea commit 3b8b207

7 files changed

+6
-808
lines changed

src/compiler/checker.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -18772,9 +18772,6 @@ namespace ts {
1877218772
return;
1877318773
}
1877418774
reportRelationError(headMessage, source, target);
18775-
if (strictNullChecks && source.flags & TypeFlags.TypeVariable && source.symbol?.declarations?.[0] && !getConstraintOfType(source as TypeVariable) && isRelatedTo(emptyObjectType, extractTypesOfKind(target, ~TypeFlags.NonPrimitive))) {
18776-
associateRelatedInfo(createDiagnosticForNode(source.symbol.declarations[0], Diagnostics.This_type_parameter_probably_needs_an_extends_object_constraint));
18777-
}
1877818775
}
1877918776

1878018777
function traceUnionsOrIntersectionsTooLarge(source: Type, target: Type): void {
@@ -19565,20 +19562,20 @@ namespace ts {
1956519562
// IndexedAccess comparisons are handled above in the `targetFlags & TypeFlage.IndexedAccess` branch
1956619563
if (!(sourceFlags & TypeFlags.IndexedAccess && targetFlags & TypeFlags.IndexedAccess)) {
1956719564
const constraint = getConstraintOfType(source as TypeVariable);
19568-
if (!strictNullChecks && (!constraint || (sourceFlags & TypeFlags.TypeParameter && constraint.flags & TypeFlags.Any))) {
19565+
if (!constraint || (sourceFlags & TypeFlags.TypeParameter && constraint.flags & TypeFlags.Any)) {
1956919566
// A type variable with no constraint is not related to the non-primitive object type.
1957019567
if (result = isRelatedTo(emptyObjectType, extractTypesOfKind(target, ~TypeFlags.NonPrimitive), RecursionFlags.Both)) {
1957119568
resetErrorInfo(saveErrorInfo);
1957219569
return result;
1957319570
}
1957419571
}
1957519572
// 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
19576-
else if (constraint && (result = isRelatedTo(constraint, target, RecursionFlags.Source, /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState))) {
19573+
else if (result = isRelatedTo(constraint, target, RecursionFlags.Source, /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState)) {
1957719574
resetErrorInfo(saveErrorInfo);
1957819575
return result;
1957919576
}
1958019577
// slower, fuller, this-instantiated check (necessary when comparing raw `this` types from base classes), see `subclassWithPolymorphicThisIsAssignable.ts` test for example
19581-
else if (constraint && (result = isRelatedTo(getTypeWithThisArgument(constraint, source), target, RecursionFlags.Source, reportErrors && !(targetFlags & sourceFlags & TypeFlags.TypeParameter), /*headMessage*/ undefined, intersectionState))) {
19578+
else if (result = isRelatedTo(getTypeWithThisArgument(constraint, source), target, RecursionFlags.Source, reportErrors && !(targetFlags & sourceFlags & TypeFlags.TypeParameter), /*headMessage*/ undefined, intersectionState)) {
1958219579
resetErrorInfo(saveErrorInfo);
1958319580
return result;
1958419581
}

src/compiler/diagnosticMessages.json

-4
Original file line numberDiff line numberDiff line change
@@ -1506,10 +1506,6 @@
15061506
"category": "Error",
15071507
"code": 2207
15081508
},
1509-
"This type parameter probably needs an `extends object` constraint.": {
1510-
"category": "Error",
1511-
"code": 2208
1512-
},
15131509

15141510
"Duplicate identifier '{0}'.": {
15151511
"category": "Error",

tests/baselines/reference/genericUnboundedTypeParamAssignability.errors.txt

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
tests/cases/compiler/genericUnboundedTypeParamAssignability.ts(2,5): error TS2339: Property 'toString' does not exist on type 'T'.
2-
tests/cases/compiler/genericUnboundedTypeParamAssignability.ts(15,6): error TS2345: Argument of type 'T' is not assignable to parameter of type '{}'.
3-
tests/cases/compiler/genericUnboundedTypeParamAssignability.ts(16,6): error TS2345: Argument of type 'T' is not assignable to parameter of type 'Record<string, any>'.
42
tests/cases/compiler/genericUnboundedTypeParamAssignability.ts(17,5): error TS2339: Property 'toString' does not exist on type 'T'.
53

64

7-
==== tests/cases/compiler/genericUnboundedTypeParamAssignability.ts (4 errors) ====
5+
==== tests/cases/compiler/genericUnboundedTypeParamAssignability.ts (2 errors) ====
86
function f1<T>(o: T) {
97
o.toString(); // error
108
~~~~~~~~
@@ -22,13 +20,7 @@ tests/cases/compiler/genericUnboundedTypeParamAssignability.ts(17,5): error TS23
2220
function user<T>(t: T) {
2321
f1(t);
2422
f2(t); // error in strict, unbounded T doesn't satisfy the constraint
25-
~
26-
!!! error TS2345: Argument of type 'T' is not assignable to parameter of type '{}'.
27-
!!! related TS2208 tests/cases/compiler/genericUnboundedTypeParamAssignability.ts:13:15: This type parameter probably needs an `extends object` constraint.
2823
f3(t); // error in strict, unbounded T doesn't satisfy the constraint
29-
~
30-
!!! error TS2345: Argument of type 'T' is not assignable to parameter of type 'Record<string, any>'.
31-
!!! related TS2208 tests/cases/compiler/genericUnboundedTypeParamAssignability.ts:13:15: This type parameter probably needs an `extends object` constraint.
3224
t.toString(); // error, for the same reason as f1()
3325
~~~~~~~~
3426
!!! error TS2339: Property 'toString' does not exist on type 'T'.

0 commit comments

Comments
 (0)