Skip to content

Commit 3799524

Browse files
committed
Add tests
1 parent 05ad13a commit 3799524

File tree

5 files changed

+1563
-0
lines changed

5 files changed

+1563
-0
lines changed
Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(9,1): error TS2322: Type 'Covariant<unknown>' is not assignable to type 'Covariant<string>'.
2+
Type 'unknown' is not assignable to type 'string'.
3+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(18,1): error TS2322: Type 'Contravariant<string>' is not assignable to type 'Contravariant<unknown>'.
4+
Type 'unknown' is not assignable to type 'string'.
5+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(28,1): error TS2322: Type 'Invariant<string>' is not assignable to type 'Invariant<unknown>'.
6+
Types of property 'f' are incompatible.
7+
Type '(x: string) => string' is not assignable to type '(x: unknown) => unknown'.
8+
Types of parameters 'x' and 'x' are incompatible.
9+
Type 'unknown' is not assignable to type 'string'.
10+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(29,1): error TS2322: Type 'Invariant<unknown>' is not assignable to type 'Invariant<string>'.
11+
The types returned by 'f(...)' are incompatible between these types.
12+
Type 'unknown' is not assignable to type 'string'.
13+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(40,17): error TS2636: Type 'Covariant1<super-T>' is not assignable to type 'Covariant1<sub-T>' as implied by variance annotation.
14+
Types of property 'x' are incompatible.
15+
Type 'super-T' is not assignable to type 'sub-T'.
16+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(44,21): error TS2636: Type 'keyof sub-T' is not assignable to type 'keyof super-T' as implied by variance annotation.
17+
Type 'string | number | symbol' is not assignable to type 'keyof super-T'.
18+
Type 'string' is not assignable to type 'keyof super-T'.
19+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(46,21): error TS2636: Type 'Contravariant2<sub-T>' is not assignable to type 'Contravariant2<super-T>' as implied by variance annotation.
20+
Types of property 'f' are incompatible.
21+
Type '(x: sub-T) => void' is not assignable to type '(x: super-T) => void'.
22+
Types of parameters 'x' and 'x' are incompatible.
23+
Type 'super-T' is not assignable to type 'sub-T'.
24+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(50,17): error TS2636: Type 'Invariant1<super-T>' is not assignable to type 'Invariant1<sub-T>' as implied by variance annotation.
25+
The types returned by 'f(...)' are incompatible between these types.
26+
Type 'super-T' is not assignable to type 'sub-T'.
27+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(54,17): error TS2636: Type 'Invariant2<sub-T>' is not assignable to type 'Invariant2<super-T>' as implied by variance annotation.
28+
Types of property 'f' are incompatible.
29+
Type '(x: sub-T) => sub-T' is not assignable to type '(x: super-T) => super-T'.
30+
Types of parameters 'x' and 'x' are incompatible.
31+
Type 'super-T' is not assignable to type 'sub-T'.
32+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(60,11): error TS2636: Type 'Foo1<super-T>' is not assignable to type 'Foo1<sub-T>' as implied by variance annotation.
33+
Types of property 'x' are incompatible.
34+
Type 'super-T' is not assignable to type 'sub-T'.
35+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(71,11): error TS2636: Type 'Foo2<sub-T>' is not assignable to type 'Foo2<super-T>' as implied by variance annotation.
36+
Types of property 'f' are incompatible.
37+
Type 'FooFn2<sub-T>' is not assignable to type 'FooFn2<super-T>'.
38+
Type 'super-T' is not assignable to type 'sub-T'.
39+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(95,10): error TS1272: 'public' modifier cannot appear on a type parameter
40+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(96,17): error TS1030: 'in' modifier already seen.
41+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(97,17): error TS1030: 'out' modifier already seen.
42+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(98,14): error TS1029: 'in' modifier must precede 'out' modifier.
43+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(100,21): error TS1273: 'in' modifier can only appear on a type parameter of a class, interface or type alias
44+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(101,21): error TS1273: 'out' modifier can only appear on a type parameter of a class, interface or type alias
45+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(104,5): error TS1273: 'in' modifier can only appear on a type parameter of a class, interface or type alias
46+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(105,5): error TS1273: 'out' modifier can only appear on a type parameter of a class, interface or type alias
47+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(116,1): error TS2322: Type 'Baz<string>' is not assignable to type 'Baz<unknown>'.
48+
Type 'unknown' is not assignable to type 'string'.
49+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(117,1): error TS2322: Type 'Baz<unknown>' is not assignable to type 'Baz<string>'.
50+
Type 'unknown' is not assignable to type 'string'.
51+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(136,7): error TS2322: Type 'Parent<unknown>' is not assignable to type 'Parent<string>'.
52+
Type 'unknown' is not assignable to type 'string'.
53+
tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts(160,68): error TS2345: Argument of type 'ActionObject<{ type: "PLAY"; value: number; }>' is not assignable to parameter of type 'ActionObject<{ type: "PLAY"; value: number; } | { type: "RESET"; }>'.
54+
Types of property 'exec' are incompatible.
55+
Type '(meta: StateNode<any, { type: "PLAY"; value: number; }>) => void' is not assignable to type '(meta: StateNode<any, { type: "PLAY"; value: number; } | { type: "RESET"; }>) => void'.
56+
Types of parameters 'meta' and 'meta' are incompatible.
57+
Type 'StateNode<any, { type: "PLAY"; value: number; } | { type: "RESET"; }>' is not assignable to type 'StateNode<any, { type: "PLAY"; value: number; }>'.
58+
Types of property '_storedEvent' are incompatible.
59+
Type '{ type: "PLAY"; value: number; } | { type: "RESET"; }' is not assignable to type '{ type: "PLAY"; value: number; }'.
60+
Type '{ type: "RESET"; }' is not assignable to type '{ type: "PLAY"; value: number; }'.
61+
62+
63+
==== tests/cases/conformance/types/typeParameters/typeParameterLists/varianceAnnotations.ts (23 errors) ====
64+
type Covariant<out T> = {
65+
x: T;
66+
}
67+
68+
declare let super_covariant: Covariant<unknown>;
69+
declare let sub_covariant: Covariant<string>;
70+
71+
super_covariant = sub_covariant;
72+
sub_covariant = super_covariant; // Error
73+
~~~~~~~~~~~~~
74+
!!! error TS2322: Type 'Covariant<unknown>' is not assignable to type 'Covariant<string>'.
75+
!!! error TS2322: Type 'unknown' is not assignable to type 'string'.
76+
77+
type Contravariant<in T> = {
78+
f: (x: T) => void;
79+
}
80+
81+
declare let super_contravariant: Contravariant<unknown>;
82+
declare let sub_contravariant: Contravariant<string>;
83+
84+
super_contravariant = sub_contravariant; // Error
85+
~~~~~~~~~~~~~~~~~~~
86+
!!! error TS2322: Type 'Contravariant<string>' is not assignable to type 'Contravariant<unknown>'.
87+
!!! error TS2322: Type 'unknown' is not assignable to type 'string'.
88+
sub_contravariant = super_contravariant;
89+
90+
type Invariant<in out T> = {
91+
f: (x: T) => T;
92+
}
93+
94+
declare let super_invariant: Invariant<unknown>;
95+
declare let sub_invariant: Invariant<string>;
96+
97+
super_invariant = sub_invariant; // Error
98+
~~~~~~~~~~~~~~~
99+
!!! error TS2322: Type 'Invariant<string>' is not assignable to type 'Invariant<unknown>'.
100+
!!! error TS2322: Types of property 'f' are incompatible.
101+
!!! error TS2322: Type '(x: string) => string' is not assignable to type '(x: unknown) => unknown'.
102+
!!! error TS2322: Types of parameters 'x' and 'x' are incompatible.
103+
!!! error TS2322: Type 'unknown' is not assignable to type 'string'.
104+
sub_invariant = super_invariant; // Error
105+
~~~~~~~~~~~~~
106+
!!! error TS2322: Type 'Invariant<unknown>' is not assignable to type 'Invariant<string>'.
107+
!!! error TS2322: The types returned by 'f(...)' are incompatible between these types.
108+
!!! error TS2322: Type 'unknown' is not assignable to type 'string'.
109+
110+
// Variance of various type constructors
111+
112+
type T10<out T> = T;
113+
type T11<in T> = keyof T;
114+
type T12<out T, out K extends keyof T> = T[K];
115+
type T13<in out T> = T[keyof T];
116+
117+
// Variance annotation errors
118+
119+
type Covariant1<in T> = { // Error
120+
~~~~
121+
!!! error TS2636: Type 'Covariant1<super-T>' is not assignable to type 'Covariant1<sub-T>' as implied by variance annotation.
122+
!!! error TS2636: Types of property 'x' are incompatible.
123+
!!! error TS2636: Type 'super-T' is not assignable to type 'sub-T'.
124+
x: T;
125+
}
126+
127+
type Contravariant1<out T> = keyof T; // Error
128+
~~~~~
129+
!!! error TS2636: Type 'keyof sub-T' is not assignable to type 'keyof super-T' as implied by variance annotation.
130+
!!! error TS2636: Type 'string | number | symbol' is not assignable to type 'keyof super-T'.
131+
!!! error TS2636: Type 'string' is not assignable to type 'keyof super-T'.
132+
133+
type Contravariant2<out T> = { // Error
134+
~~~~~
135+
!!! error TS2636: Type 'Contravariant2<sub-T>' is not assignable to type 'Contravariant2<super-T>' as implied by variance annotation.
136+
!!! error TS2636: Types of property 'f' are incompatible.
137+
!!! error TS2636: Type '(x: sub-T) => void' is not assignable to type '(x: super-T) => void'.
138+
!!! error TS2636: Types of parameters 'x' and 'x' are incompatible.
139+
!!! error TS2636: Type 'super-T' is not assignable to type 'sub-T'.
140+
f: (x: T) => void;
141+
}
142+
143+
type Invariant1<in T> = { // Error
144+
~~~~
145+
!!! error TS2636: Type 'Invariant1<super-T>' is not assignable to type 'Invariant1<sub-T>' as implied by variance annotation.
146+
!!! error TS2636: The types returned by 'f(...)' are incompatible between these types.
147+
!!! error TS2636: Type 'super-T' is not assignable to type 'sub-T'.
148+
f: (x: T) => T;
149+
}
150+
151+
type Invariant2<out T> = { // Error
152+
~~~~~
153+
!!! error TS2636: Type 'Invariant2<sub-T>' is not assignable to type 'Invariant2<super-T>' as implied by variance annotation.
154+
!!! error TS2636: Types of property 'f' are incompatible.
155+
!!! error TS2636: Type '(x: sub-T) => sub-T' is not assignable to type '(x: super-T) => super-T'.
156+
!!! error TS2636: Types of parameters 'x' and 'x' are incompatible.
157+
!!! error TS2636: Type 'super-T' is not assignable to type 'sub-T'.
158+
f: (x: T) => T;
159+
}
160+
161+
// Variance in circular types
162+
163+
type Foo1<in T> = { // Error
164+
~~~~
165+
!!! error TS2636: Type 'Foo1<super-T>' is not assignable to type 'Foo1<sub-T>' as implied by variance annotation.
166+
!!! error TS2636: Types of property 'x' are incompatible.
167+
!!! error TS2636: Type 'super-T' is not assignable to type 'sub-T'.
168+
x: T;
169+
f: FooFn1<T>;
170+
}
171+
172+
type FooFn1<T> = (foo: Bar1<T[]>) => void;
173+
174+
type Bar1<T> = {
175+
value: Foo1<T[]>;
176+
}
177+
178+
type Foo2<out T> = { // Error
179+
~~~~~
180+
!!! error TS2636: Type 'Foo2<sub-T>' is not assignable to type 'Foo2<super-T>' as implied by variance annotation.
181+
!!! error TS2636: Types of property 'f' are incompatible.
182+
!!! error TS2636: Type 'FooFn2<sub-T>' is not assignable to type 'FooFn2<super-T>'.
183+
!!! error TS2636: Type 'super-T' is not assignable to type 'sub-T'.
184+
x: T;
185+
f: FooFn2<T>;
186+
}
187+
188+
type FooFn2<T> = (foo: Bar2<T[]>) => void;
189+
190+
type Bar2<T> = {
191+
value: Foo2<T[]>;
192+
}
193+
194+
type Foo3<in out T> = {
195+
x: T;
196+
f: FooFn3<T>;
197+
}
198+
199+
type FooFn3<T> = (foo: Bar3<T[]>) => void;
200+
201+
type Bar3<T> = {
202+
value: Foo3<T[]>;
203+
}
204+
205+
// Wrong modifier usage
206+
207+
type T20<public T> = T; // Error
208+
~~~~~~
209+
!!! error TS1272: 'public' modifier cannot appear on a type parameter
210+
type T21<in out in T> = T; // Error
211+
~~
212+
!!! error TS1030: 'in' modifier already seen.
213+
type T22<in out out T> = T; // Error
214+
~~~
215+
!!! error TS1030: 'out' modifier already seen.
216+
type T23<out in T> = T; // Error
217+
~~
218+
!!! error TS1029: 'in' modifier must precede 'out' modifier.
219+
220+
declare function f1<in T>(x: T): void; // Error
221+
~~
222+
!!! error TS1273: 'in' modifier can only appear on a type parameter of a class, interface or type alias
223+
declare function f2<out T>(): T; // Error
224+
~~~
225+
!!! error TS1273: 'out' modifier can only appear on a type parameter of a class, interface or type alias
226+
227+
class C {
228+
in a = 0; // Error
229+
~~
230+
!!! error TS1273: 'in' modifier can only appear on a type parameter of a class, interface or type alias
231+
out b = 0; // Error
232+
~~~
233+
!!! error TS1273: 'out' modifier can only appear on a type parameter of a class, interface or type alias
234+
}
235+
236+
// Interface merging
237+
238+
interface Baz<out T> {}
239+
interface Baz<in T> {}
240+
241+
declare let baz1: Baz<unknown>;
242+
declare let baz2: Baz<string>;
243+
244+
baz1 = baz2; // Error
245+
~~~~
246+
!!! error TS2322: Type 'Baz<string>' is not assignable to type 'Baz<unknown>'.
247+
!!! error TS2322: Type 'unknown' is not assignable to type 'string'.
248+
baz2 = baz1; // Error
249+
~~~~
250+
!!! error TS2322: Type 'Baz<unknown>' is not assignable to type 'Baz<string>'.
251+
!!! error TS2322: Type 'unknown' is not assignable to type 'string'.
252+
253+
// Repro from #44572
254+
255+
interface Parent<out A> {
256+
child: Child<A> | null;
257+
parent: Parent<A> | null;
258+
}
259+
260+
interface Child<A, B = unknown> extends Parent<A> {
261+
readonly a: A;
262+
readonly b: B;
263+
}
264+
265+
function fn<A>(inp: Child<A>) {
266+
const a: Child<unknown> = inp;
267+
}
268+
269+
const pu: Parent<unknown> = { child: { a: 0, b: 0, child: null, parent: null }, parent: null };
270+
const notString: Parent<string> = pu; // Error
271+
~~~~~~~~~
272+
!!! error TS2322: Type 'Parent<unknown>' is not assignable to type 'Parent<string>'.
273+
!!! error TS2322: Type 'unknown' is not assignable to type 'string'.
274+
275+
// Repro from comment in #44572
276+
277+
declare class StateNode<TContext, in out TEvent extends { type: string }> {
278+
_storedEvent: TEvent;
279+
_action: ActionObject<TEvent>;
280+
_state: StateNode<TContext, any>;
281+
}
282+
283+
interface ActionObject<TEvent extends { type: string }> {
284+
exec: (meta: StateNode<any, TEvent>) => void;
285+
}
286+
287+
declare function createMachine<TEvent extends { type: string }>(action: ActionObject<TEvent>): StateNode<any, any>;
288+
289+
declare function interpret<TContext>(machine: StateNode<TContext, any>): void;
290+
291+
const machine = createMachine({} as any);
292+
293+
interpret(machine);
294+
295+
declare const qq: ActionObject<{ type: "PLAY"; value: number }>;
296+
297+
createMachine<{ type: "PLAY"; value: number } | { type: "RESET" }>(qq); // Error
298+
~~
299+
!!! error TS2345: Argument of type 'ActionObject<{ type: "PLAY"; value: number; }>' is not assignable to parameter of type 'ActionObject<{ type: "PLAY"; value: number; } | { type: "RESET"; }>'.
300+
!!! error TS2345: Types of property 'exec' are incompatible.
301+
!!! error TS2345: Type '(meta: StateNode<any, { type: "PLAY"; value: number; }>) => void' is not assignable to type '(meta: StateNode<any, { type: "PLAY"; value: number; } | { type: "RESET"; }>) => void'.
302+
!!! error TS2345: Types of parameters 'meta' and 'meta' are incompatible.
303+
!!! error TS2345: Type 'StateNode<any, { type: "PLAY"; value: number; } | { type: "RESET"; }>' is not assignable to type 'StateNode<any, { type: "PLAY"; value: number; }>'.
304+
!!! error TS2345: Types of property '_storedEvent' are incompatible.
305+
!!! error TS2345: Type '{ type: "PLAY"; value: number; } | { type: "RESET"; }' is not assignable to type '{ type: "PLAY"; value: number; }'.
306+
!!! error TS2345: Type '{ type: "RESET"; }' is not assignable to type '{ type: "PLAY"; value: number; }'.
307+

0 commit comments

Comments
 (0)