Skip to content

Commit 939b5b4

Browse files
committed
Add tests
1 parent 68ef6c9 commit 939b5b4

File tree

4 files changed

+329
-0
lines changed

4 files changed

+329
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//// [coAndContraVariantInferences.ts]
2+
type A = { kind: 'a' };
3+
type B = { kind: 'b' };
4+
5+
declare const a: A;
6+
declare const b: B;
7+
8+
declare function fab(arg: A | B): void;
9+
10+
declare function foo<T>(x: { kind: T }, f: (arg: { kind: T }) => void): void;
11+
12+
foo(a, fab);
13+
foo(b, fab);
14+
15+
// Repro from #45603
16+
17+
interface Action<TName extends string,TPayload> {
18+
name: TName,
19+
payload: TPayload
20+
}
21+
22+
const actionA = { payload: 'any-string' } as Action<'ACTION_A', string>;
23+
const actionB = { payload: true } as Action<'ACTION_B', boolean>;
24+
25+
function call<TName extends string,TPayload>(
26+
action: Action<TName,TPayload>,
27+
fn: (action: Action<TName,TPayload>)=> any,
28+
) {
29+
fn(action);
30+
}
31+
32+
const printFn = (action: typeof actionA | typeof actionB)=> console.log(action);
33+
34+
call(actionA, printFn);
35+
call(actionB, printFn);
36+
37+
38+
//// [coAndContraVariantInferences.js]
39+
"use strict";
40+
foo(a, fab);
41+
foo(b, fab);
42+
var actionA = { payload: 'any-string' };
43+
var actionB = { payload: true };
44+
function call(action, fn) {
45+
fn(action);
46+
}
47+
var printFn = function (action) { return console.log(action); };
48+
call(actionA, printFn);
49+
call(actionB, printFn);
50+
51+
52+
//// [coAndContraVariantInferences.d.ts]
53+
declare type A = {
54+
kind: 'a';
55+
};
56+
declare type B = {
57+
kind: 'b';
58+
};
59+
declare const a: A;
60+
declare const b: B;
61+
declare function fab(arg: A | B): void;
62+
declare function foo<T>(x: {
63+
kind: T;
64+
}, f: (arg: {
65+
kind: T;
66+
}) => void): void;
67+
interface Action<TName extends string, TPayload> {
68+
name: TName;
69+
payload: TPayload;
70+
}
71+
declare const actionA: Action<"ACTION_A", string>;
72+
declare const actionB: Action<"ACTION_B", boolean>;
73+
declare function call<TName extends string, TPayload>(action: Action<TName, TPayload>, fn: (action: Action<TName, TPayload>) => any): void;
74+
declare const printFn: (action: typeof actionA | typeof actionB) => void;
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
=== tests/cases/compiler/coAndContraVariantInferences.ts ===
2+
type A = { kind: 'a' };
3+
>A : Symbol(A, Decl(coAndContraVariantInferences.ts, 0, 0))
4+
>kind : Symbol(kind, Decl(coAndContraVariantInferences.ts, 0, 10))
5+
6+
type B = { kind: 'b' };
7+
>B : Symbol(B, Decl(coAndContraVariantInferences.ts, 0, 23))
8+
>kind : Symbol(kind, Decl(coAndContraVariantInferences.ts, 1, 10))
9+
10+
declare const a: A;
11+
>a : Symbol(a, Decl(coAndContraVariantInferences.ts, 3, 13))
12+
>A : Symbol(A, Decl(coAndContraVariantInferences.ts, 0, 0))
13+
14+
declare const b: B;
15+
>b : Symbol(b, Decl(coAndContraVariantInferences.ts, 4, 13))
16+
>B : Symbol(B, Decl(coAndContraVariantInferences.ts, 0, 23))
17+
18+
declare function fab(arg: A | B): void;
19+
>fab : Symbol(fab, Decl(coAndContraVariantInferences.ts, 4, 19))
20+
>arg : Symbol(arg, Decl(coAndContraVariantInferences.ts, 6, 21))
21+
>A : Symbol(A, Decl(coAndContraVariantInferences.ts, 0, 0))
22+
>B : Symbol(B, Decl(coAndContraVariantInferences.ts, 0, 23))
23+
24+
declare function foo<T>(x: { kind: T }, f: (arg: { kind: T }) => void): void;
25+
>foo : Symbol(foo, Decl(coAndContraVariantInferences.ts, 6, 39))
26+
>T : Symbol(T, Decl(coAndContraVariantInferences.ts, 8, 21))
27+
>x : Symbol(x, Decl(coAndContraVariantInferences.ts, 8, 24))
28+
>kind : Symbol(kind, Decl(coAndContraVariantInferences.ts, 8, 28))
29+
>T : Symbol(T, Decl(coAndContraVariantInferences.ts, 8, 21))
30+
>f : Symbol(f, Decl(coAndContraVariantInferences.ts, 8, 39))
31+
>arg : Symbol(arg, Decl(coAndContraVariantInferences.ts, 8, 44))
32+
>kind : Symbol(kind, Decl(coAndContraVariantInferences.ts, 8, 50))
33+
>T : Symbol(T, Decl(coAndContraVariantInferences.ts, 8, 21))
34+
35+
foo(a, fab);
36+
>foo : Symbol(foo, Decl(coAndContraVariantInferences.ts, 6, 39))
37+
>a : Symbol(a, Decl(coAndContraVariantInferences.ts, 3, 13))
38+
>fab : Symbol(fab, Decl(coAndContraVariantInferences.ts, 4, 19))
39+
40+
foo(b, fab);
41+
>foo : Symbol(foo, Decl(coAndContraVariantInferences.ts, 6, 39))
42+
>b : Symbol(b, Decl(coAndContraVariantInferences.ts, 4, 13))
43+
>fab : Symbol(fab, Decl(coAndContraVariantInferences.ts, 4, 19))
44+
45+
// Repro from #45603
46+
47+
interface Action<TName extends string,TPayload> {
48+
>Action : Symbol(Action, Decl(coAndContraVariantInferences.ts, 11, 12))
49+
>TName : Symbol(TName, Decl(coAndContraVariantInferences.ts, 15, 17))
50+
>TPayload : Symbol(TPayload, Decl(coAndContraVariantInferences.ts, 15, 38))
51+
52+
name: TName,
53+
>name : Symbol(Action.name, Decl(coAndContraVariantInferences.ts, 15, 49))
54+
>TName : Symbol(TName, Decl(coAndContraVariantInferences.ts, 15, 17))
55+
56+
payload: TPayload
57+
>payload : Symbol(Action.payload, Decl(coAndContraVariantInferences.ts, 16, 16))
58+
>TPayload : Symbol(TPayload, Decl(coAndContraVariantInferences.ts, 15, 38))
59+
}
60+
61+
const actionA = { payload: 'any-string' } as Action<'ACTION_A', string>;
62+
>actionA : Symbol(actionA, Decl(coAndContraVariantInferences.ts, 20, 5))
63+
>payload : Symbol(payload, Decl(coAndContraVariantInferences.ts, 20, 17))
64+
>Action : Symbol(Action, Decl(coAndContraVariantInferences.ts, 11, 12))
65+
66+
const actionB = { payload: true } as Action<'ACTION_B', boolean>;
67+
>actionB : Symbol(actionB, Decl(coAndContraVariantInferences.ts, 21, 5))
68+
>payload : Symbol(payload, Decl(coAndContraVariantInferences.ts, 21, 17))
69+
>Action : Symbol(Action, Decl(coAndContraVariantInferences.ts, 11, 12))
70+
71+
function call<TName extends string,TPayload>(
72+
>call : Symbol(call, Decl(coAndContraVariantInferences.ts, 21, 65))
73+
>TName : Symbol(TName, Decl(coAndContraVariantInferences.ts, 23, 14))
74+
>TPayload : Symbol(TPayload, Decl(coAndContraVariantInferences.ts, 23, 35))
75+
76+
action: Action<TName,TPayload>,
77+
>action : Symbol(action, Decl(coAndContraVariantInferences.ts, 23, 45))
78+
>Action : Symbol(Action, Decl(coAndContraVariantInferences.ts, 11, 12))
79+
>TName : Symbol(TName, Decl(coAndContraVariantInferences.ts, 23, 14))
80+
>TPayload : Symbol(TPayload, Decl(coAndContraVariantInferences.ts, 23, 35))
81+
82+
fn: (action: Action<TName,TPayload>)=> any,
83+
>fn : Symbol(fn, Decl(coAndContraVariantInferences.ts, 24, 33))
84+
>action : Symbol(action, Decl(coAndContraVariantInferences.ts, 25, 7))
85+
>Action : Symbol(Action, Decl(coAndContraVariantInferences.ts, 11, 12))
86+
>TName : Symbol(TName, Decl(coAndContraVariantInferences.ts, 23, 14))
87+
>TPayload : Symbol(TPayload, Decl(coAndContraVariantInferences.ts, 23, 35))
88+
89+
) {
90+
fn(action);
91+
>fn : Symbol(fn, Decl(coAndContraVariantInferences.ts, 24, 33))
92+
>action : Symbol(action, Decl(coAndContraVariantInferences.ts, 23, 45))
93+
}
94+
95+
const printFn = (action: typeof actionA | typeof actionB)=> console.log(action);
96+
>printFn : Symbol(printFn, Decl(coAndContraVariantInferences.ts, 30, 5))
97+
>action : Symbol(action, Decl(coAndContraVariantInferences.ts, 30, 17))
98+
>actionA : Symbol(actionA, Decl(coAndContraVariantInferences.ts, 20, 5))
99+
>actionB : Symbol(actionB, Decl(coAndContraVariantInferences.ts, 21, 5))
100+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
101+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
102+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
103+
>action : Symbol(action, Decl(coAndContraVariantInferences.ts, 30, 17))
104+
105+
call(actionA, printFn);
106+
>call : Symbol(call, Decl(coAndContraVariantInferences.ts, 21, 65))
107+
>actionA : Symbol(actionA, Decl(coAndContraVariantInferences.ts, 20, 5))
108+
>printFn : Symbol(printFn, Decl(coAndContraVariantInferences.ts, 30, 5))
109+
110+
call(actionB, printFn);
111+
>call : Symbol(call, Decl(coAndContraVariantInferences.ts, 21, 65))
112+
>actionB : Symbol(actionB, Decl(coAndContraVariantInferences.ts, 21, 5))
113+
>printFn : Symbol(printFn, Decl(coAndContraVariantInferences.ts, 30, 5))
114+
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
=== tests/cases/compiler/coAndContraVariantInferences.ts ===
2+
type A = { kind: 'a' };
3+
>A : A
4+
>kind : "a"
5+
6+
type B = { kind: 'b' };
7+
>B : B
8+
>kind : "b"
9+
10+
declare const a: A;
11+
>a : A
12+
13+
declare const b: B;
14+
>b : B
15+
16+
declare function fab(arg: A | B): void;
17+
>fab : (arg: A | B) => void
18+
>arg : A | B
19+
20+
declare function foo<T>(x: { kind: T }, f: (arg: { kind: T }) => void): void;
21+
>foo : <T>(x: { kind: T;}, f: (arg: { kind: T;}) => void) => void
22+
>x : { kind: T; }
23+
>kind : T
24+
>f : (arg: { kind: T;}) => void
25+
>arg : { kind: T; }
26+
>kind : T
27+
28+
foo(a, fab);
29+
>foo(a, fab) : void
30+
>foo : <T>(x: { kind: T; }, f: (arg: { kind: T; }) => void) => void
31+
>a : A
32+
>fab : (arg: A | B) => void
33+
34+
foo(b, fab);
35+
>foo(b, fab) : void
36+
>foo : <T>(x: { kind: T; }, f: (arg: { kind: T; }) => void) => void
37+
>b : B
38+
>fab : (arg: A | B) => void
39+
40+
// Repro from #45603
41+
42+
interface Action<TName extends string,TPayload> {
43+
name: TName,
44+
>name : TName
45+
46+
payload: TPayload
47+
>payload : TPayload
48+
}
49+
50+
const actionA = { payload: 'any-string' } as Action<'ACTION_A', string>;
51+
>actionA : Action<"ACTION_A", string>
52+
>{ payload: 'any-string' } as Action<'ACTION_A', string> : Action<"ACTION_A", string>
53+
>{ payload: 'any-string' } : { payload: string; }
54+
>payload : string
55+
>'any-string' : "any-string"
56+
57+
const actionB = { payload: true } as Action<'ACTION_B', boolean>;
58+
>actionB : Action<"ACTION_B", boolean>
59+
>{ payload: true } as Action<'ACTION_B', boolean> : Action<"ACTION_B", boolean>
60+
>{ payload: true } : { payload: true; }
61+
>payload : true
62+
>true : true
63+
64+
function call<TName extends string,TPayload>(
65+
>call : <TName extends string, TPayload>(action: Action<TName, TPayload>, fn: (action: Action<TName, TPayload>) => any) => void
66+
67+
action: Action<TName,TPayload>,
68+
>action : Action<TName, TPayload>
69+
70+
fn: (action: Action<TName,TPayload>)=> any,
71+
>fn : (action: Action<TName, TPayload>) => any
72+
>action : Action<TName, TPayload>
73+
74+
) {
75+
fn(action);
76+
>fn(action) : any
77+
>fn : (action: Action<TName, TPayload>) => any
78+
>action : Action<TName, TPayload>
79+
}
80+
81+
const printFn = (action: typeof actionA | typeof actionB)=> console.log(action);
82+
>printFn : (action: typeof actionA | typeof actionB) => void
83+
>(action: typeof actionA | typeof actionB)=> console.log(action) : (action: typeof actionA | typeof actionB) => void
84+
>action : Action<"ACTION_A", string> | Action<"ACTION_B", boolean>
85+
>actionA : Action<"ACTION_A", string>
86+
>actionB : Action<"ACTION_B", boolean>
87+
>console.log(action) : void
88+
>console.log : (...data: any[]) => void
89+
>console : Console
90+
>log : (...data: any[]) => void
91+
>action : Action<"ACTION_A", string> | Action<"ACTION_B", boolean>
92+
93+
call(actionA, printFn);
94+
>call(actionA, printFn) : void
95+
>call : <TName extends string, TPayload>(action: Action<TName, TPayload>, fn: (action: Action<TName, TPayload>) => any) => void
96+
>actionA : Action<"ACTION_A", string>
97+
>printFn : (action: Action<"ACTION_A", string> | Action<"ACTION_B", boolean>) => void
98+
99+
call(actionB, printFn);
100+
>call(actionB, printFn) : void
101+
>call : <TName extends string, TPayload>(action: Action<TName, TPayload>, fn: (action: Action<TName, TPayload>) => any) => void
102+
>actionB : Action<"ACTION_B", boolean>
103+
>printFn : (action: Action<"ACTION_A", string> | Action<"ACTION_B", boolean>) => void
104+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// @strict: true
2+
// @declaration: true
3+
4+
type A = { kind: 'a' };
5+
type B = { kind: 'b' };
6+
7+
declare const a: A;
8+
declare const b: B;
9+
10+
declare function fab(arg: A | B): void;
11+
12+
declare function foo<T>(x: { kind: T }, f: (arg: { kind: T }) => void): void;
13+
14+
foo(a, fab);
15+
foo(b, fab);
16+
17+
// Repro from #45603
18+
19+
interface Action<TName extends string,TPayload> {
20+
name: TName,
21+
payload: TPayload
22+
}
23+
24+
const actionA = { payload: 'any-string' } as Action<'ACTION_A', string>;
25+
const actionB = { payload: true } as Action<'ACTION_B', boolean>;
26+
27+
function call<TName extends string,TPayload>(
28+
action: Action<TName,TPayload>,
29+
fn: (action: Action<TName,TPayload>)=> any,
30+
) {
31+
fn(action);
32+
}
33+
34+
const printFn = (action: typeof actionA | typeof actionB)=> console.log(action);
35+
36+
call(actionA, printFn);
37+
call(actionB, printFn);

0 commit comments

Comments
 (0)