File: varianceAnnotations.js

package info (click to toggle)
node-typescript 5.0.4%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 459,116 kB
  • sloc: javascript: 1,972,754; makefile: 6; sh: 1
file content (336 lines) | stat: -rw-r--r-- 7,407 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//// [varianceAnnotations.ts]
type Covariant<out T> = {
    x: T;
}

declare let super_covariant: Covariant<unknown>;
declare let sub_covariant: Covariant<string>;

super_covariant = sub_covariant;
sub_covariant = super_covariant;  // Error

type Contravariant<in T> = {
    f: (x: T) => void;
}

declare let super_contravariant: Contravariant<unknown>;
declare let sub_contravariant: Contravariant<string>;

super_contravariant = sub_contravariant;  // Error
sub_contravariant = super_contravariant;

type Invariant<in out T> = {
    f: (x: T) => T;
}

declare let super_invariant: Invariant<unknown>;
declare let sub_invariant: Invariant<string>;

super_invariant = sub_invariant;  // Error
sub_invariant = super_invariant;  // Error

// Variance of various type constructors

type T10<out T> = T;
type T11<in T> = keyof T;
type T12<out T, out K extends keyof T> = T[K];
type T13<in out T> = T[keyof T];

// Variance annotation errors

type Covariant1<in T> = {  // Error
    x: T;
}

type Contravariant1<out T> = keyof T;  // Error

type Contravariant2<out T> = {  // Error
    f: (x: T) => void;
}

type Invariant1<in T> = {  // Error
    f: (x: T) => T;
}

type Invariant2<out T> = {  // Error
    f: (x: T) => T;
}

// Variance in circular types

type Foo1<in T> = {  // Error
    x: T;
    f: FooFn1<T>;
}

type FooFn1<T> = (foo: Bar1<T[]>) => void;

type Bar1<T> = {
    value: Foo1<T[]>;
}

type Foo2<out T> = {  // Error
    x: T;
    f: FooFn2<T>;
}

type FooFn2<T> = (foo: Bar2<T[]>) => void;

type Bar2<T> = {
    value: Foo2<T[]>;
}

type Foo3<in out T> = {
    x: T;
    f: FooFn3<T>;
}

type FooFn3<T> = (foo: Bar3<T[]>) => void;

type Bar3<T> = {
    value: Foo3<T[]>;
}

// Wrong modifier usage

type T20<public T> = T;  // Error
type T21<in out in T> = T;  // Error
type T22<in out out T> = T;  // Error
type T23<out in T> = T;  // Error

declare function f1<in T>(x: T): void;  // Error
declare function f2<out T>(): T;  // Error

class C {
    in a = 0;  // Error
    out b = 0;  // Error
}

// Interface merging

interface Baz<out T> {}
interface Baz<in T> {}

declare let baz1: Baz<unknown>;
declare let baz2: Baz<string>;

baz1 = baz2;  // Error
baz2 = baz1;  // Error

// Repro from #44572

interface Parent<out A> {
    child: Child<A> | null;
    parent: Parent<A> | null;
}

interface Child<A, B = unknown> extends Parent<A> {
    readonly a: A;
    readonly b: B;
}

function fn<A>(inp: Child<A>) {
    const a: Child<unknown> = inp;
}

const pu: Parent<unknown> = { child: { a: 0, b: 0, child: null, parent: null }, parent: null };
const notString: Parent<string> = pu;  // Error

// Repro from comment in #44572

declare class StateNode<TContext, in out TEvent extends { type: string }> {
    _storedEvent: TEvent;
    _action: ActionObject<TEvent>;
    _state: StateNode<TContext, any>;
}

interface ActionObject<TEvent extends { type: string }> {
    exec: (meta: StateNode<any, TEvent>) => void;
}

declare function createMachine<TEvent extends { type: string }>(action: ActionObject<TEvent>): StateNode<any, any>;

declare function interpret<TContext>(machine: StateNode<TContext, any>): void;

const machine = createMachine({} as any);

interpret(machine);

declare const qq: ActionObject<{ type: "PLAY"; value: number }>;

createMachine<{ type: "PLAY"; value: number } | { type: "RESET" }>(qq);  // Error

// Repros from #48618

let Anon = class <out T> {
    foo(): InstanceType<(typeof Anon<T>)> {
        return this;
    }
}

let OuterC = class C<out T> {
    foo(): C<T> {
        return this;
    }
}


//// [varianceAnnotations.js]
"use strict";
super_covariant = sub_covariant;
sub_covariant = super_covariant; // Error
super_contravariant = sub_contravariant; // Error
sub_contravariant = super_contravariant;
super_invariant = sub_invariant; // Error
sub_invariant = super_invariant; // Error
var C = /** @class */ (function () {
    function C() {
        this.a = 0; // Error
        this.b = 0; // Error
    }
    return C;
}());
baz1 = baz2; // Error
baz2 = baz1; // Error
function fn(inp) {
    var a = inp;
}
var pu = { child: { a: 0, b: 0, child: null, parent: null }, parent: null };
var notString = pu; // Error
var machine = createMachine({});
interpret(machine);
createMachine(qq); // Error
// Repros from #48618
var Anon = /** @class */ (function () {
    function class_1() {
    }
    class_1.prototype.foo = function () {
        return this;
    };
    return class_1;
}());
var OuterC = /** @class */ (function () {
    function C() {
    }
    C.prototype.foo = function () {
        return this;
    };
    return C;
}());


//// [varianceAnnotations.d.ts]
type Covariant<out T> = {
    x: T;
};
declare let super_covariant: Covariant<unknown>;
declare let sub_covariant: Covariant<string>;
type Contravariant<in T> = {
    f: (x: T) => void;
};
declare let super_contravariant: Contravariant<unknown>;
declare let sub_contravariant: Contravariant<string>;
type Invariant<in out T> = {
    f: (x: T) => T;
};
declare let super_invariant: Invariant<unknown>;
declare let sub_invariant: Invariant<string>;
type T10<out T> = T;
type T11<in T> = keyof T;
type T12<out T, out K extends keyof T> = T[K];
type T13<in out T> = T[keyof T];
type Covariant1<in T> = {
    x: T;
};
type Contravariant1<out T> = keyof T;
type Contravariant2<out T> = {
    f: (x: T) => void;
};
type Invariant1<in T> = {
    f: (x: T) => T;
};
type Invariant2<out T> = {
    f: (x: T) => T;
};
type Foo1<in T> = {
    x: T;
    f: FooFn1<T>;
};
type FooFn1<T> = (foo: Bar1<T[]>) => void;
type Bar1<T> = {
    value: Foo1<T[]>;
};
type Foo2<out T> = {
    x: T;
    f: FooFn2<T>;
};
type FooFn2<T> = (foo: Bar2<T[]>) => void;
type Bar2<T> = {
    value: Foo2<T[]>;
};
type Foo3<in out T> = {
    x: T;
    f: FooFn3<T>;
};
type FooFn3<T> = (foo: Bar3<T[]>) => void;
type Bar3<T> = {
    value: Foo3<T[]>;
};
type T20<public T> = T;
type T21<in out in T> = T;
type T22<in out out T> = T;
type T23<out in T> = T;
declare function f1<in T>(x: T): void;
declare function f2<out T>(): T;
declare class C {
    in a: number;
    out b: number;
}
interface Baz<out T> {
}
interface Baz<in T> {
}
declare let baz1: Baz<unknown>;
declare let baz2: Baz<string>;
interface Parent<out A> {
    child: Child<A> | null;
    parent: Parent<A> | null;
}
interface Child<A, B = unknown> extends Parent<A> {
    readonly a: A;
    readonly b: B;
}
declare function fn<A>(inp: Child<A>): void;
declare const pu: Parent<unknown>;
declare const notString: Parent<string>;
declare class StateNode<TContext, in out TEvent extends {
    type: string;
}> {
    _storedEvent: TEvent;
    _action: ActionObject<TEvent>;
    _state: StateNode<TContext, any>;
}
interface ActionObject<TEvent extends {
    type: string;
}> {
    exec: (meta: StateNode<any, TEvent>) => void;
}
declare function createMachine<TEvent extends {
    type: string;
}>(action: ActionObject<TEvent>): StateNode<any, any>;
declare function interpret<TContext>(machine: StateNode<TContext, any>): void;
declare const machine: StateNode<any, any>;
declare const qq: ActionObject<{
    type: "PLAY";
    value: number;
}>;
declare let Anon: {
    new <out T>(): {
        foo(): any;
    };
};
declare let OuterC: {
    new <out T>(): {
        foo(): any;
    };
};