Skip to content

Commit 061e41a

Browse files
committed
Default invariant type parameters to constraint, contravariant to never
1 parent 5d4e7d7 commit 061e41a

6 files changed

+107
-79
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10153,8 +10153,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1015310153
case VarianceFlags.Independent:
1015410154
case VarianceFlags.Bivariant:
1015510155
return anyType;
10156-
case VarianceFlags.Invariant:
10157-
return unknownType;
10156+
case VarianceFlags.Contravariant:
10157+
return neverType;
1015810158
}
1015910159
return getBaseConstraintOfType(typeParameter) || unknownType;
1016010160
});

tests/baselines/reference/strictInstanceOfTypeParametersWithVarianceAnnotations.errors.txt

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts(21
66
Type 'string' is not assignable to type 'never'.
77
tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts(24,9): error TS2322: Type 'ConstrainedCovariant<string>' is not assignable to type 'ConstrainedCovariant<"literal">'.
88
Type 'string' is not assignable to type '"literal"'.
9+
tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts(34,9): error TS2322: Type 'UnconstrainedContravariant<never>' is not assignable to type 'UnconstrainedContravariant<unknown>'.
10+
Type 'unknown' is not assignable to type 'never'.
11+
tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts(36,9): error TS2322: Type 'UnconstrainedContravariant<never>' is not assignable to type 'UnconstrainedContravariant<any>'.
12+
Type 'any' is not assignable to type 'never'.
13+
tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts(37,9): error TS2322: Type 'UnconstrainedContravariant<never>' is not assignable to type 'UnconstrainedContravariant<string>'.
14+
Type 'string' is not assignable to type 'never'.
15+
tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts(38,9): error TS2322: Type 'UnconstrainedContravariant<never>' is not assignable to type 'UnconstrainedContravariant<"literal">'.
16+
Type 'string' is not assignable to type 'never'.
17+
tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts(49,9): error TS2322: Type 'ConstrainedContravariant<never>' is not assignable to type 'ConstrainedContravariant<any>'.
18+
Type 'any' is not assignable to type 'never'.
19+
tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts(50,9): error TS2322: Type 'ConstrainedContravariant<never>' is not assignable to type 'ConstrainedContravariant<string>'.
20+
Type 'string' is not assignable to type 'never'.
21+
tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts(51,9): error TS2322: Type 'ConstrainedContravariant<never>' is not assignable to type 'ConstrainedContravariant<"literal">'.
22+
Type 'string' is not assignable to type 'never'.
923
tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts(62,9): error TS2322: Type 'UnconstrainedInvariant<unknown>' is not assignable to type 'UnconstrainedInvariant<never>'.
1024
The types returned by 'f(...)' are incompatible between these types.
1125
Type 'unknown' is not assignable to type 'never'.
@@ -15,18 +29,15 @@ tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts(64
1529
tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts(65,9): error TS2322: Type 'UnconstrainedInvariant<unknown>' is not assignable to type 'UnconstrainedInvariant<"literal">'.
1630
The types returned by 'f(...)' are incompatible between these types.
1731
Type 'unknown' is not assignable to type '"literal"'.
18-
tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts(75,9): error TS2322: Type 'ConstrainedInvariant<unknown>' is not assignable to type 'ConstrainedInvariant<never>'.
32+
tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts(75,9): error TS2322: Type 'ConstrainedInvariant<string>' is not assignable to type 'ConstrainedInvariant<never>'.
1933
The types returned by 'f(...)' are incompatible between these types.
20-
Type 'unknown' is not assignable to type 'never'.
21-
tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts(77,9): error TS2322: Type 'ConstrainedInvariant<unknown>' is not assignable to type 'ConstrainedInvariant<string>'.
34+
Type 'string' is not assignable to type 'never'.
35+
tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts(78,9): error TS2322: Type 'ConstrainedInvariant<string>' is not assignable to type 'ConstrainedInvariant<"literal">'.
2236
The types returned by 'f(...)' are incompatible between these types.
23-
Type 'unknown' is not assignable to type 'string'.
24-
tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts(78,9): error TS2322: Type 'ConstrainedInvariant<unknown>' is not assignable to type 'ConstrainedInvariant<"literal">'.
25-
The types returned by 'f(...)' are incompatible between these types.
26-
Type 'unknown' is not assignable to type '"literal"'.
37+
Type 'string' is not assignable to type '"literal"'.
2738

2839

29-
==== tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts (10 errors) ====
40+
==== tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts (16 errors) ====
3041
class UnconstrainedCovariant<out T> {
3142
x: T;
3243
}
@@ -72,11 +83,23 @@ tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts(78
7283
declare const unc_contravariant: unknown;
7384

7485
if (unc_contravariant instanceof UnconstrainedContravariant) {
75-
let unknown_covariant: UnconstrainedContravariant<unknown> = unc_contravariant;
86+
let unknown_covariant: UnconstrainedContravariant<unknown> = unc_contravariant; // Error
87+
~~~~~~~~~~~~~~~~~
88+
!!! error TS2322: Type 'UnconstrainedContravariant<never>' is not assignable to type 'UnconstrainedContravariant<unknown>'.
89+
!!! error TS2322: Type 'unknown' is not assignable to type 'never'.
7690
let never_covariant: UnconstrainedContravariant<never> = unc_contravariant;
77-
let any_covariant: UnconstrainedContravariant<any> = unc_contravariant;
78-
let constraint_covariant: UnconstrainedContravariant<string> = unc_contravariant;
79-
let sub_covariant: UnconstrainedContravariant<"literal"> = unc_contravariant;
91+
let any_covariant: UnconstrainedContravariant<any> = unc_contravariant; // Error
92+
~~~~~~~~~~~~~
93+
!!! error TS2322: Type 'UnconstrainedContravariant<never>' is not assignable to type 'UnconstrainedContravariant<any>'.
94+
!!! error TS2322: Type 'any' is not assignable to type 'never'.
95+
let constraint_covariant: UnconstrainedContravariant<string> = unc_contravariant; // Error
96+
~~~~~~~~~~~~~~~~~~~~
97+
!!! error TS2322: Type 'UnconstrainedContravariant<never>' is not assignable to type 'UnconstrainedContravariant<string>'.
98+
!!! error TS2322: Type 'string' is not assignable to type 'never'.
99+
let sub_covariant: UnconstrainedContravariant<"literal"> = unc_contravariant; // Error
100+
~~~~~~~~~~~~~
101+
!!! error TS2322: Type 'UnconstrainedContravariant<never>' is not assignable to type 'UnconstrainedContravariant<"literal">'.
102+
!!! error TS2322: Type 'string' is not assignable to type 'never'.
80103
}
81104

82105
class ConstrainedContravariant<in T extends string> {
@@ -87,9 +110,18 @@ tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts(78
87110

88111
if (con_contravariant instanceof ConstrainedContravariant) {
89112
let never_covariant: ConstrainedContravariant<never> = con_contravariant;
90-
let any_covariant: ConstrainedContravariant<any> = con_contravariant;
91-
let constraint_covariant: ConstrainedContravariant<string> = con_contravariant;
92-
let sub_covariant: ConstrainedContravariant<"literal"> = con_contravariant;
113+
let any_covariant: ConstrainedContravariant<any> = con_contravariant; // Error
114+
~~~~~~~~~~~~~
115+
!!! error TS2322: Type 'ConstrainedContravariant<never>' is not assignable to type 'ConstrainedContravariant<any>'.
116+
!!! error TS2322: Type 'any' is not assignable to type 'never'.
117+
let constraint_covariant: ConstrainedContravariant<string> = con_contravariant; // Error
118+
~~~~~~~~~~~~~~~~~~~~
119+
!!! error TS2322: Type 'ConstrainedContravariant<never>' is not assignable to type 'ConstrainedContravariant<string>'.
120+
!!! error TS2322: Type 'string' is not assignable to type 'never'.
121+
let sub_covariant: ConstrainedContravariant<"literal"> = con_contravariant; // Error
122+
~~~~~~~~~~~~~
123+
!!! error TS2322: Type 'ConstrainedContravariant<never>' is not assignable to type 'ConstrainedContravariant<"literal">'.
124+
!!! error TS2322: Type 'string' is not assignable to type 'never'.
93125
}
94126

95127
class UnconstrainedInvariant<in out T> {
@@ -127,19 +159,15 @@ tests/cases/compiler/strictInstanceOfTypeParametersWithVarianceAnnotations.ts(78
127159
if (con_invariant instanceof ConstrainedInvariant) {
128160
let never_covariant: ConstrainedInvariant<never> = con_invariant; // Error
129161
~~~~~~~~~~~~~~~
130-
!!! error TS2322: Type 'ConstrainedInvariant<unknown>' is not assignable to type 'ConstrainedInvariant<never>'.
162+
!!! error TS2322: Type 'ConstrainedInvariant<string>' is not assignable to type 'ConstrainedInvariant<never>'.
131163
!!! error TS2322: The types returned by 'f(...)' are incompatible between these types.
132-
!!! error TS2322: Type 'unknown' is not assignable to type 'never'.
164+
!!! error TS2322: Type 'string' is not assignable to type 'never'.
133165
let any_covariant: ConstrainedInvariant<any> = con_invariant;
134-
let constraint_covariant: ConstrainedInvariant<string> = con_invariant; // Error
135-
~~~~~~~~~~~~~~~~~~~~
136-
!!! error TS2322: Type 'ConstrainedInvariant<unknown>' is not assignable to type 'ConstrainedInvariant<string>'.
137-
!!! error TS2322: The types returned by 'f(...)' are incompatible between these types.
138-
!!! error TS2322: Type 'unknown' is not assignable to type 'string'.
166+
let constraint_covariant: ConstrainedInvariant<string> = con_invariant;
139167
let sub_covariant: ConstrainedInvariant<"literal"> = con_invariant; // Error
140168
~~~~~~~~~~~~~
141-
!!! error TS2322: Type 'ConstrainedInvariant<unknown>' is not assignable to type 'ConstrainedInvariant<"literal">'.
169+
!!! error TS2322: Type 'ConstrainedInvariant<string>' is not assignable to type 'ConstrainedInvariant<"literal">'.
142170
!!! error TS2322: The types returned by 'f(...)' are incompatible between these types.
143-
!!! error TS2322: Type 'unknown' is not assignable to type '"literal"'.
171+
!!! error TS2322: Type 'string' is not assignable to type '"literal"'.
144172
}
145173

tests/baselines/reference/strictInstanceOfTypeParametersWithVarianceAnnotations.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ class UnconstrainedContravariant<in T> {
3232
declare const unc_contravariant: unknown;
3333

3434
if (unc_contravariant instanceof UnconstrainedContravariant) {
35-
let unknown_covariant: UnconstrainedContravariant<unknown> = unc_contravariant;
35+
let unknown_covariant: UnconstrainedContravariant<unknown> = unc_contravariant; // Error
3636
let never_covariant: UnconstrainedContravariant<never> = unc_contravariant;
37-
let any_covariant: UnconstrainedContravariant<any> = unc_contravariant;
38-
let constraint_covariant: UnconstrainedContravariant<string> = unc_contravariant;
39-
let sub_covariant: UnconstrainedContravariant<"literal"> = unc_contravariant;
37+
let any_covariant: UnconstrainedContravariant<any> = unc_contravariant; // Error
38+
let constraint_covariant: UnconstrainedContravariant<string> = unc_contravariant; // Error
39+
let sub_covariant: UnconstrainedContravariant<"literal"> = unc_contravariant; // Error
4040
}
4141

4242
class ConstrainedContravariant<in T extends string> {
@@ -47,9 +47,9 @@ declare const con_contravariant: unknown;
4747

4848
if (con_contravariant instanceof ConstrainedContravariant) {
4949
let never_covariant: ConstrainedContravariant<never> = con_contravariant;
50-
let any_covariant: ConstrainedContravariant<any> = con_contravariant;
51-
let constraint_covariant: ConstrainedContravariant<string> = con_contravariant;
52-
let sub_covariant: ConstrainedContravariant<"literal"> = con_contravariant;
50+
let any_covariant: ConstrainedContravariant<any> = con_contravariant; // Error
51+
let constraint_covariant: ConstrainedContravariant<string> = con_contravariant; // Error
52+
let sub_covariant: ConstrainedContravariant<"literal"> = con_contravariant; // Error
5353
}
5454

5555
class UnconstrainedInvariant<in out T> {
@@ -75,7 +75,7 @@ declare const con_invariant: unknown;
7575
if (con_invariant instanceof ConstrainedInvariant) {
7676
let never_covariant: ConstrainedInvariant<never> = con_invariant; // Error
7777
let any_covariant: ConstrainedInvariant<any> = con_invariant;
78-
let constraint_covariant: ConstrainedInvariant<string> = con_invariant; // Error
78+
let constraint_covariant: ConstrainedInvariant<string> = con_invariant;
7979
let sub_covariant: ConstrainedInvariant<"literal"> = con_invariant; // Error
8080
}
8181

@@ -109,11 +109,11 @@ var UnconstrainedContravariant = /** @class */ (function () {
109109
return UnconstrainedContravariant;
110110
}());
111111
if (unc_contravariant instanceof UnconstrainedContravariant) {
112-
var unknown_covariant = unc_contravariant;
112+
var unknown_covariant = unc_contravariant; // Error
113113
var never_covariant = unc_contravariant;
114-
var any_covariant = unc_contravariant;
115-
var constraint_covariant = unc_contravariant;
116-
var sub_covariant = unc_contravariant;
114+
var any_covariant = unc_contravariant; // Error
115+
var constraint_covariant = unc_contravariant; // Error
116+
var sub_covariant = unc_contravariant; // Error
117117
}
118118
var ConstrainedContravariant = /** @class */ (function () {
119119
function ConstrainedContravariant() {
@@ -122,9 +122,9 @@ var ConstrainedContravariant = /** @class */ (function () {
122122
}());
123123
if (con_contravariant instanceof ConstrainedContravariant) {
124124
var never_covariant = con_contravariant;
125-
var any_covariant = con_contravariant;
126-
var constraint_covariant = con_contravariant;
127-
var sub_covariant = con_contravariant;
125+
var any_covariant = con_contravariant; // Error
126+
var constraint_covariant = con_contravariant; // Error
127+
var sub_covariant = con_contravariant; // Error
128128
}
129129
var UnconstrainedInvariant = /** @class */ (function () {
130130
function UnconstrainedInvariant() {
@@ -146,6 +146,6 @@ var ConstrainedInvariant = /** @class */ (function () {
146146
if (con_invariant instanceof ConstrainedInvariant) {
147147
var never_covariant = con_invariant; // Error
148148
var any_covariant = con_invariant;
149-
var constraint_covariant = con_invariant; // Error
149+
var constraint_covariant = con_invariant;
150150
var sub_covariant = con_invariant; // Error
151151
}

0 commit comments

Comments
 (0)