Skip to content

Commit 29d92ed

Browse files
authored
Revert "Allow variance annotations on generic references (#56418)" (#59793)
1 parent fe2cb8e commit 29d92ed

File tree

6 files changed

+348
-85
lines changed

6 files changed

+348
-85
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41147,7 +41147,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4114741147
const modifiers = getTypeParameterModifiers(typeParameter) & (ModifierFlags.In | ModifierFlags.Out);
4114841148
if (modifiers) {
4114941149
const symbol = getSymbolOfDeclaration(node.parent);
41150-
if (isTypeAliasDeclaration(node.parent) && !(getObjectFlags(getDeclaredTypeOfSymbol(symbol)) & (ObjectFlags.Reference | ObjectFlags.Anonymous | ObjectFlags.Mapped))) {
41150+
if (isTypeAliasDeclaration(node.parent) && !(getObjectFlags(getDeclaredTypeOfSymbol(symbol)) & (ObjectFlags.Anonymous | ObjectFlags.Mapped))) {
4115141151
error(node, Diagnostics.Variance_annotations_are_only_supported_in_type_aliases_for_object_function_constructor_and_mapped_types);
4115241152
}
4115341153
else if (modifiers === ModifierFlags.In || modifiers === ModifierFlags.Out) {
Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,54 @@
11
varianceReferences.ts(3,32): error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.
2-
varianceReferences.ts(8,28): error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.
3-
varianceReferences.ts(14,32): error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.
2+
varianceReferences.ts(9,1): error TS2322: Type '1 | 2' is not assignable to type '1'.
3+
Type '2' is not assignable to type '1'.
4+
varianceReferences.ts(14,28): error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.
5+
varianceReferences.ts(19,1): error TS2322: Type '1 | 2' is not assignable to type '1'.
6+
Type '2' is not assignable to type '1'.
7+
varianceReferences.ts(26,32): error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.
8+
varianceReferences.ts(31,1): error TS2322: Type '1 | 2' is not assignable to type '1'.
9+
Type '2' is not assignable to type '1'.
10+
varianceReferences.ts(38,20): error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.
11+
varianceReferences.ts(43,1): error TS2322: Type 'VarianceShape<1 | 2>' is not assignable to type 'VarianceShape<1>'.
12+
Type '1 | 2' is not assignable to type '1'.
13+
Type '2' is not assignable to type '1'.
14+
varianceReferences.ts(53,24): error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.
15+
varianceReferences.ts(58,1): error TS2322: Type 'VarianceDeepShape<1 | 2>' is not assignable to type 'VarianceDeepShape<1>'.
16+
Type '1 | 2' is not assignable to type '1'.
17+
Type '2' is not assignable to type '1'.
418

519

6-
==== varianceReferences.ts (3 errors) ====
20+
==== varianceReferences.ts (10 errors) ====
721
type NumericConstraint<Value extends number> = Value;
822

923
type VarianceConstrainedNumber<in out Value extends number> =
1024
~~~~~~~~~~~~~~~~~~~~~~~~~~~
1125
!!! error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.
1226
NumericConstraint<Value>;
1327

28+
declare let vcn1: VarianceConstrainedNumber<1>;
29+
declare let vcn12: VarianceConstrainedNumber<1 | 2>;
30+
31+
vcn1 = vcn12;
32+
~~~~
33+
!!! error TS2322: Type '1 | 2' is not assignable to type '1'.
34+
!!! error TS2322: Type '2' is not assignable to type '1'.
35+
vcn12 = vcn1;
36+
1437
type Unconstrained<Value> = Value;
1538

1639
type VarianceUnconstrained<in out Value> = Unconstrained<Value>;
1740
~~~~~~~~~~~~
1841
!!! error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.
1942

43+
declare let vu1: VarianceUnconstrained<1>;
44+
declare let vu12: VarianceUnconstrained<1 | 2>;
45+
46+
vu1 = vu12;
47+
~~~
48+
!!! error TS2322: Type '1 | 2' is not assignable to type '1'.
49+
!!! error TS2322: Type '2' is not assignable to type '1'.
50+
vu12 = vu1;
51+
2052
type Level3of3Unconstrained<Value> = Value;
2153
type Level2of3Unconstrained<Value> = Level3of3Unconstrained<Value>;
2254
type Level1of3Unconstrained<Value> = Level2of3Unconstrained<Value>;
@@ -25,11 +57,32 @@ varianceReferences.ts(14,32): error TS2637: Variance annotations are only suppor
2557
~~~~~~~~~~~~
2658
!!! error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.
2759

60+
declare let vdu1: VarianceDeepUnconstrained<1>;
61+
declare let vdu12: VarianceDeepUnconstrained<1 | 2>;
62+
63+
vdu1 = vdu12;
64+
~~~~
65+
!!! error TS2322: Type '1 | 2' is not assignable to type '1'.
66+
!!! error TS2322: Type '2' is not assignable to type '1'.
67+
vdu12 = vdu1;
68+
2869
interface Shape<Value> {
2970
value: Value;
3071
}
3172

3273
type VarianceShape<in out Value> = Shape<Value>;
74+
~~~~~~~~~~~~
75+
!!! error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.
76+
77+
declare let vs1: VarianceShape<1>;
78+
declare let vs12: VarianceShape<1 | 2>;
79+
80+
vs1 = vs12;
81+
~~~
82+
!!! error TS2322: Type 'VarianceShape<1 | 2>' is not assignable to type 'VarianceShape<1>'.
83+
!!! error TS2322: Type '1 | 2' is not assignable to type '1'.
84+
!!! error TS2322: Type '2' is not assignable to type '1'.
85+
vs12 = vs1;
3386

3487
interface Level3of3Shape<Value> {
3588
value: Value;
@@ -39,4 +92,16 @@ varianceReferences.ts(14,32): error TS2637: Variance annotations are only suppor
3992
type Level1of3Shape<Value> = Level2of3Shape<Value>;
4093

4194
type VarianceDeepShape<in out Value> = Level1of3Shape<Value>;
95+
~~~~~~~~~~~~
96+
!!! error TS2637: Variance annotations are only supported in type aliases for object, function, constructor, and mapped types.
97+
98+
declare let vds1: VarianceDeepShape<1>;
99+
declare let vds12: VarianceDeepShape<1 | 2>;
100+
101+
vds1 = vds12;
102+
~~~~
103+
!!! error TS2322: Type 'VarianceDeepShape<1 | 2>' is not assignable to type 'VarianceDeepShape<1>'.
104+
!!! error TS2322: Type '1 | 2' is not assignable to type '1'.
105+
!!! error TS2322: Type '2' is not assignable to type '1'.
106+
vds12 = vds1;
42107

tests/baselines/reference/varianceReferences.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

tests/baselines/reference/varianceReferences.symbols

Lines changed: 126 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,79 +14,159 @@ type VarianceConstrainedNumber<in out Value extends number> =
1414
>NumericConstraint : Symbol(NumericConstraint, Decl(varianceReferences.ts, 0, 0))
1515
>Value : Symbol(Value, Decl(varianceReferences.ts, 2, 31))
1616

17+
declare let vcn1: VarianceConstrainedNumber<1>;
18+
>vcn1 : Symbol(vcn1, Decl(varianceReferences.ts, 5, 11))
19+
>VarianceConstrainedNumber : Symbol(VarianceConstrainedNumber, Decl(varianceReferences.ts, 0, 53))
20+
21+
declare let vcn12: VarianceConstrainedNumber<1 | 2>;
22+
>vcn12 : Symbol(vcn12, Decl(varianceReferences.ts, 6, 11))
23+
>VarianceConstrainedNumber : Symbol(VarianceConstrainedNumber, Decl(varianceReferences.ts, 0, 53))
24+
25+
vcn1 = vcn12;
26+
>vcn1 : Symbol(vcn1, Decl(varianceReferences.ts, 5, 11))
27+
>vcn12 : Symbol(vcn12, Decl(varianceReferences.ts, 6, 11))
28+
29+
vcn12 = vcn1;
30+
>vcn12 : Symbol(vcn12, Decl(varianceReferences.ts, 6, 11))
31+
>vcn1 : Symbol(vcn1, Decl(varianceReferences.ts, 5, 11))
32+
1733
type Unconstrained<Value> = Value;
18-
>Unconstrained : Symbol(Unconstrained, Decl(varianceReferences.ts, 3, 27))
19-
>Value : Symbol(Value, Decl(varianceReferences.ts, 5, 19))
20-
>Value : Symbol(Value, Decl(varianceReferences.ts, 5, 19))
34+
>Unconstrained : Symbol(Unconstrained, Decl(varianceReferences.ts, 9, 13))
35+
>Value : Symbol(Value, Decl(varianceReferences.ts, 11, 19))
36+
>Value : Symbol(Value, Decl(varianceReferences.ts, 11, 19))
2137

2238
type VarianceUnconstrained<in out Value> = Unconstrained<Value>;
23-
>VarianceUnconstrained : Symbol(VarianceUnconstrained, Decl(varianceReferences.ts, 5, 34))
24-
>Value : Symbol(Value, Decl(varianceReferences.ts, 7, 27))
25-
>Unconstrained : Symbol(Unconstrained, Decl(varianceReferences.ts, 3, 27))
26-
>Value : Symbol(Value, Decl(varianceReferences.ts, 7, 27))
39+
>VarianceUnconstrained : Symbol(VarianceUnconstrained, Decl(varianceReferences.ts, 11, 34))
40+
>Value : Symbol(Value, Decl(varianceReferences.ts, 13, 27))
41+
>Unconstrained : Symbol(Unconstrained, Decl(varianceReferences.ts, 9, 13))
42+
>Value : Symbol(Value, Decl(varianceReferences.ts, 13, 27))
43+
44+
declare let vu1: VarianceUnconstrained<1>;
45+
>vu1 : Symbol(vu1, Decl(varianceReferences.ts, 15, 11))
46+
>VarianceUnconstrained : Symbol(VarianceUnconstrained, Decl(varianceReferences.ts, 11, 34))
47+
48+
declare let vu12: VarianceUnconstrained<1 | 2>;
49+
>vu12 : Symbol(vu12, Decl(varianceReferences.ts, 16, 11))
50+
>VarianceUnconstrained : Symbol(VarianceUnconstrained, Decl(varianceReferences.ts, 11, 34))
51+
52+
vu1 = vu12;
53+
>vu1 : Symbol(vu1, Decl(varianceReferences.ts, 15, 11))
54+
>vu12 : Symbol(vu12, Decl(varianceReferences.ts, 16, 11))
55+
56+
vu12 = vu1;
57+
>vu12 : Symbol(vu12, Decl(varianceReferences.ts, 16, 11))
58+
>vu1 : Symbol(vu1, Decl(varianceReferences.ts, 15, 11))
2759

2860
type Level3of3Unconstrained<Value> = Value;
29-
>Level3of3Unconstrained : Symbol(Level3of3Unconstrained, Decl(varianceReferences.ts, 7, 64))
30-
>Value : Symbol(Value, Decl(varianceReferences.ts, 9, 28))
31-
>Value : Symbol(Value, Decl(varianceReferences.ts, 9, 28))
61+
>Level3of3Unconstrained : Symbol(Level3of3Unconstrained, Decl(varianceReferences.ts, 19, 11))
62+
>Value : Symbol(Value, Decl(varianceReferences.ts, 21, 28))
63+
>Value : Symbol(Value, Decl(varianceReferences.ts, 21, 28))
3264

3365
type Level2of3Unconstrained<Value> = Level3of3Unconstrained<Value>;
34-
>Level2of3Unconstrained : Symbol(Level2of3Unconstrained, Decl(varianceReferences.ts, 9, 43))
35-
>Value : Symbol(Value, Decl(varianceReferences.ts, 10, 28))
36-
>Level3of3Unconstrained : Symbol(Level3of3Unconstrained, Decl(varianceReferences.ts, 7, 64))
37-
>Value : Symbol(Value, Decl(varianceReferences.ts, 10, 28))
66+
>Level2of3Unconstrained : Symbol(Level2of3Unconstrained, Decl(varianceReferences.ts, 21, 43))
67+
>Value : Symbol(Value, Decl(varianceReferences.ts, 22, 28))
68+
>Level3of3Unconstrained : Symbol(Level3of3Unconstrained, Decl(varianceReferences.ts, 19, 11))
69+
>Value : Symbol(Value, Decl(varianceReferences.ts, 22, 28))
3870

3971
type Level1of3Unconstrained<Value> = Level2of3Unconstrained<Value>;
40-
>Level1of3Unconstrained : Symbol(Level1of3Unconstrained, Decl(varianceReferences.ts, 10, 67))
41-
>Value : Symbol(Value, Decl(varianceReferences.ts, 11, 28))
42-
>Level2of3Unconstrained : Symbol(Level2of3Unconstrained, Decl(varianceReferences.ts, 9, 43))
43-
>Value : Symbol(Value, Decl(varianceReferences.ts, 11, 28))
72+
>Level1of3Unconstrained : Symbol(Level1of3Unconstrained, Decl(varianceReferences.ts, 22, 67))
73+
>Value : Symbol(Value, Decl(varianceReferences.ts, 23, 28))
74+
>Level2of3Unconstrained : Symbol(Level2of3Unconstrained, Decl(varianceReferences.ts, 21, 43))
75+
>Value : Symbol(Value, Decl(varianceReferences.ts, 23, 28))
4476

4577
type VarianceDeepUnconstrained<in out Value> = Level1of3Unconstrained<Value>;
46-
>VarianceDeepUnconstrained : Symbol(VarianceDeepUnconstrained, Decl(varianceReferences.ts, 11, 67))
47-
>Value : Symbol(Value, Decl(varianceReferences.ts, 13, 31))
48-
>Level1of3Unconstrained : Symbol(Level1of3Unconstrained, Decl(varianceReferences.ts, 10, 67))
49-
>Value : Symbol(Value, Decl(varianceReferences.ts, 13, 31))
78+
>VarianceDeepUnconstrained : Symbol(VarianceDeepUnconstrained, Decl(varianceReferences.ts, 23, 67))
79+
>Value : Symbol(Value, Decl(varianceReferences.ts, 25, 31))
80+
>Level1of3Unconstrained : Symbol(Level1of3Unconstrained, Decl(varianceReferences.ts, 22, 67))
81+
>Value : Symbol(Value, Decl(varianceReferences.ts, 25, 31))
82+
83+
declare let vdu1: VarianceDeepUnconstrained<1>;
84+
>vdu1 : Symbol(vdu1, Decl(varianceReferences.ts, 27, 11))
85+
>VarianceDeepUnconstrained : Symbol(VarianceDeepUnconstrained, Decl(varianceReferences.ts, 23, 67))
86+
87+
declare let vdu12: VarianceDeepUnconstrained<1 | 2>;
88+
>vdu12 : Symbol(vdu12, Decl(varianceReferences.ts, 28, 11))
89+
>VarianceDeepUnconstrained : Symbol(VarianceDeepUnconstrained, Decl(varianceReferences.ts, 23, 67))
90+
91+
vdu1 = vdu12;
92+
>vdu1 : Symbol(vdu1, Decl(varianceReferences.ts, 27, 11))
93+
>vdu12 : Symbol(vdu12, Decl(varianceReferences.ts, 28, 11))
94+
95+
vdu12 = vdu1;
96+
>vdu12 : Symbol(vdu12, Decl(varianceReferences.ts, 28, 11))
97+
>vdu1 : Symbol(vdu1, Decl(varianceReferences.ts, 27, 11))
5098

5199
interface Shape<Value> {
52-
>Shape : Symbol(Shape, Decl(varianceReferences.ts, 13, 77))
53-
>Value : Symbol(Value, Decl(varianceReferences.ts, 15, 16))
100+
>Shape : Symbol(Shape, Decl(varianceReferences.ts, 31, 13))
101+
>Value : Symbol(Value, Decl(varianceReferences.ts, 33, 16))
54102

55103
value: Value;
56-
>value : Symbol(Shape.value, Decl(varianceReferences.ts, 15, 24))
57-
>Value : Symbol(Value, Decl(varianceReferences.ts, 15, 16))
104+
>value : Symbol(Shape.value, Decl(varianceReferences.ts, 33, 24))
105+
>Value : Symbol(Value, Decl(varianceReferences.ts, 33, 16))
58106
}
59107

60108
type VarianceShape<in out Value> = Shape<Value>;
61-
>VarianceShape : Symbol(VarianceShape, Decl(varianceReferences.ts, 17, 1))
62-
>Value : Symbol(Value, Decl(varianceReferences.ts, 19, 19))
63-
>Shape : Symbol(Shape, Decl(varianceReferences.ts, 13, 77))
64-
>Value : Symbol(Value, Decl(varianceReferences.ts, 19, 19))
109+
>VarianceShape : Symbol(VarianceShape, Decl(varianceReferences.ts, 35, 1))
110+
>Value : Symbol(Value, Decl(varianceReferences.ts, 37, 19))
111+
>Shape : Symbol(Shape, Decl(varianceReferences.ts, 31, 13))
112+
>Value : Symbol(Value, Decl(varianceReferences.ts, 37, 19))
113+
114+
declare let vs1: VarianceShape<1>;
115+
>vs1 : Symbol(vs1, Decl(varianceReferences.ts, 39, 11))
116+
>VarianceShape : Symbol(VarianceShape, Decl(varianceReferences.ts, 35, 1))
117+
118+
declare let vs12: VarianceShape<1 | 2>;
119+
>vs12 : Symbol(vs12, Decl(varianceReferences.ts, 40, 11))
120+
>VarianceShape : Symbol(VarianceShape, Decl(varianceReferences.ts, 35, 1))
121+
122+
vs1 = vs12;
123+
>vs1 : Symbol(vs1, Decl(varianceReferences.ts, 39, 11))
124+
>vs12 : Symbol(vs12, Decl(varianceReferences.ts, 40, 11))
125+
126+
vs12 = vs1;
127+
>vs12 : Symbol(vs12, Decl(varianceReferences.ts, 40, 11))
128+
>vs1 : Symbol(vs1, Decl(varianceReferences.ts, 39, 11))
65129

66130
interface Level3of3Shape<Value> {
67-
>Level3of3Shape : Symbol(Level3of3Shape, Decl(varianceReferences.ts, 19, 48))
68-
>Value : Symbol(Value, Decl(varianceReferences.ts, 21, 25))
131+
>Level3of3Shape : Symbol(Level3of3Shape, Decl(varianceReferences.ts, 43, 11))
132+
>Value : Symbol(Value, Decl(varianceReferences.ts, 45, 25))
69133

70134
value: Value;
71-
>value : Symbol(Level3of3Shape.value, Decl(varianceReferences.ts, 21, 33))
72-
>Value : Symbol(Value, Decl(varianceReferences.ts, 21, 25))
135+
>value : Symbol(Level3of3Shape.value, Decl(varianceReferences.ts, 45, 33))
136+
>Value : Symbol(Value, Decl(varianceReferences.ts, 45, 25))
73137
}
74138

75139
type Level2of3Shape<Value> = Level3of3Shape<Value>;
76-
>Level2of3Shape : Symbol(Level2of3Shape, Decl(varianceReferences.ts, 23, 1))
77-
>Value : Symbol(Value, Decl(varianceReferences.ts, 25, 20))
78-
>Level3of3Shape : Symbol(Level3of3Shape, Decl(varianceReferences.ts, 19, 48))
79-
>Value : Symbol(Value, Decl(varianceReferences.ts, 25, 20))
140+
>Level2of3Shape : Symbol(Level2of3Shape, Decl(varianceReferences.ts, 47, 1))
141+
>Value : Symbol(Value, Decl(varianceReferences.ts, 49, 20))
142+
>Level3of3Shape : Symbol(Level3of3Shape, Decl(varianceReferences.ts, 43, 11))
143+
>Value : Symbol(Value, Decl(varianceReferences.ts, 49, 20))
80144

81145
type Level1of3Shape<Value> = Level2of3Shape<Value>;
82-
>Level1of3Shape : Symbol(Level1of3Shape, Decl(varianceReferences.ts, 25, 51))
83-
>Value : Symbol(Value, Decl(varianceReferences.ts, 26, 20))
84-
>Level2of3Shape : Symbol(Level2of3Shape, Decl(varianceReferences.ts, 23, 1))
85-
>Value : Symbol(Value, Decl(varianceReferences.ts, 26, 20))
146+
>Level1of3Shape : Symbol(Level1of3Shape, Decl(varianceReferences.ts, 49, 51))
147+
>Value : Symbol(Value, Decl(varianceReferences.ts, 50, 20))
148+
>Level2of3Shape : Symbol(Level2of3Shape, Decl(varianceReferences.ts, 47, 1))
149+
>Value : Symbol(Value, Decl(varianceReferences.ts, 50, 20))
86150

87151
type VarianceDeepShape<in out Value> = Level1of3Shape<Value>;
88-
>VarianceDeepShape : Symbol(VarianceDeepShape, Decl(varianceReferences.ts, 26, 51))
89-
>Value : Symbol(Value, Decl(varianceReferences.ts, 28, 23))
90-
>Level1of3Shape : Symbol(Level1of3Shape, Decl(varianceReferences.ts, 25, 51))
91-
>Value : Symbol(Value, Decl(varianceReferences.ts, 28, 23))
152+
>VarianceDeepShape : Symbol(VarianceDeepShape, Decl(varianceReferences.ts, 50, 51))
153+
>Value : Symbol(Value, Decl(varianceReferences.ts, 52, 23))
154+
>Level1of3Shape : Symbol(Level1of3Shape, Decl(varianceReferences.ts, 49, 51))
155+
>Value : Symbol(Value, Decl(varianceReferences.ts, 52, 23))
156+
157+
declare let vds1: VarianceDeepShape<1>;
158+
>vds1 : Symbol(vds1, Decl(varianceReferences.ts, 54, 11))
159+
>VarianceDeepShape : Symbol(VarianceDeepShape, Decl(varianceReferences.ts, 50, 51))
160+
161+
declare let vds12: VarianceDeepShape<1 | 2>;
162+
>vds12 : Symbol(vds12, Decl(varianceReferences.ts, 55, 11))
163+
>VarianceDeepShape : Symbol(VarianceDeepShape, Decl(varianceReferences.ts, 50, 51))
164+
165+
vds1 = vds12;
166+
>vds1 : Symbol(vds1, Decl(varianceReferences.ts, 54, 11))
167+
>vds12 : Symbol(vds12, Decl(varianceReferences.ts, 55, 11))
168+
169+
vds12 = vds1;
170+
>vds12 : Symbol(vds12, Decl(varianceReferences.ts, 55, 11))
171+
>vds1 : Symbol(vds1, Decl(varianceReferences.ts, 54, 11))
92172

0 commit comments

Comments
 (0)