Skip to content

Commit a16b1ad

Browse files
committedMay 26, 2022
Make the unconstrained type parameter and {} assignability rule not apply under strictNullChecks (microsoft#48366)
* Make the unconstrained type parameter and {} assignability rule not apply under strictNullChecks * Fix lint, PR feedback # Conflicts: # src/compiler/checker.ts # tests/baselines/reference/unknownType1.errors.txt
1 parent 954e80a commit a16b1ad

10 files changed

+824
-13
lines changed
 

Diff for: ‎src/compiler/checker.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -18812,6 +18812,9 @@ namespace ts {
1881218812
return;
1881318813
}
1881418814
reportRelationError(headMessage, source, target);
18815+
if (strictNullChecks && source.flags & TypeFlags.TypeVariable && source.symbol?.declarations?.[0] && !getConstraintOfType(source as TypeVariable) && isRelatedTo(emptyObjectType, extractTypesOfKind(target, ~TypeFlags.NonPrimitive))) {
18816+
associateRelatedInfo(createDiagnosticForNode(source.symbol.declarations[0], Diagnostics.This_type_parameter_probably_needs_an_extends_object_constraint));
18817+
}
1881518818
}
1881618819

1881718820
function traceUnionsOrIntersectionsTooLarge(source: Type, target: Type): void {
@@ -19616,13 +19619,6 @@ namespace ts {
1961619619
// IndexedAccess comparisons are handled above in the `targetFlags & TypeFlage.IndexedAccess` branch
1961719620
if (!(sourceFlags & TypeFlags.IndexedAccess && targetFlags & TypeFlags.IndexedAccess)) {
1961819621
const constraint = getConstraintOfType(source as TypeVariable) || unknownType;
19619-
if (!getConstraintOfType(source as TypeVariable) || (sourceFlags & TypeFlags.TypeParameter && constraint.flags & TypeFlags.Any)) {
19620-
// A type variable with no constraint is not related to the non-primitive object type.
19621-
if (result = isRelatedTo(emptyObjectType, extractTypesOfKind(target, ~TypeFlags.NonPrimitive), RecursionFlags.Both)) {
19622-
resetErrorInfo(saveErrorInfo);
19623-
return result;
19624-
}
19625-
}
1962619622
// 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
1962719623
if (result = isRelatedTo(constraint, target, RecursionFlags.Source, /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState)) {
1962819624
resetErrorInfo(saveErrorInfo);

Diff for: ‎src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,10 @@
15261526
"category": "Error",
15271527
"code": 2207
15281528
},
1529+
"This type parameter probably needs an `extends object` constraint.": {
1530+
"category": "Error",
1531+
"code": 2208
1532+
},
15291533

15301534
"The project root is ambiguous, but is required to resolve export map entry '{0}' in file '{1}'. Supply the `rootDir` compiler option to disambiguate.": {
15311535
"category": "Error",

Diff for: ‎tests/baselines/reference/conditionalTypes1.errors.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
tests/cases/conformance/types/conditional/conditionalTypes1.ts(12,5): error TS2322: Type 'T' is not assignable to type 'NonNullable<T>'.
2+
Type 'T' is not assignable to type '{}'.
13
tests/cases/conformance/types/conditional/conditionalTypes1.ts(17,5): error TS2322: Type 'T' is not assignable to type 'NonNullable<T>'.
24
Type 'string | undefined' is not assignable to type 'NonNullable<T>'.
35
Type 'undefined' is not assignable to type 'NonNullable<T>'.
@@ -66,7 +68,7 @@ tests/cases/conformance/types/conditional/conditionalTypes1.ts(288,43): error TS
6668
Type 'boolean' is not assignable to type 'true'.
6769

6870

69-
==== tests/cases/conformance/types/conditional/conditionalTypes1.ts (19 errors) ====
71+
==== tests/cases/conformance/types/conditional/conditionalTypes1.ts (20 errors) ====
7072
type T00 = Exclude<"a" | "b" | "c" | "d", "a" | "c" | "f">; // "b" | "d"
7173
type T01 = Extract<"a" | "b" | "c" | "d", "a" | "c" | "f">; // "a" | "c"
7274

@@ -79,6 +81,10 @@ tests/cases/conformance/types/conditional/conditionalTypes1.ts(288,43): error TS
7981
function f1<T>(x: T, y: NonNullable<T>) {
8082
x = y;
8183
y = x; // Error
84+
~
85+
!!! error TS2322: Type 'T' is not assignable to type 'NonNullable<T>'.
86+
!!! error TS2322: Type 'T' is not assignable to type '{}'.
87+
!!! related TS2208 tests/cases/conformance/types/conditional/conditionalTypes1.ts:10:13: This type parameter probably needs an `extends object` constraint.
8288
}
8389

8490
function f2<T extends string | undefined>(x: T, y: NonNullable<T>) {

Diff for: ‎tests/baselines/reference/genericUnboundedTypeParamAssignability.errors.txt

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
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>'.
24
tests/cases/compiler/genericUnboundedTypeParamAssignability.ts(17,5): error TS2339: Property 'toString' does not exist on type 'T'.
35

46

5-
==== tests/cases/compiler/genericUnboundedTypeParamAssignability.ts (2 errors) ====
7+
==== tests/cases/compiler/genericUnboundedTypeParamAssignability.ts (4 errors) ====
68
function f1<T>(o: T) {
79
o.toString(); // error
810
~~~~~~~~
@@ -20,7 +22,13 @@ tests/cases/compiler/genericUnboundedTypeParamAssignability.ts(17,5): error TS23
2022
function user<T>(t: T) {
2123
f1(t);
2224
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.
2328
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.
2432
t.toString(); // error, for the same reason as f1()
2533
~~~~~~~~
2634
!!! error TS2339: Property 'toString' does not exist on type 'T'.

0 commit comments

Comments
 (0)