Skip to content

Commit 50f84b1

Browse files
committed
Add tests
1 parent 11543d7 commit 50f84b1

File tree

3 files changed

+246
-0
lines changed

3 files changed

+246
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/covariantCallbacks.ts(12,5): error TS2322: Type 'P<A>' is not assignable to type 'P<B>'.
2+
Type 'A' is not assignable to type 'B'.
3+
Property 'b' is missing in type 'A'.
4+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/covariantCallbacks.ts(17,5): error TS2322: Type 'Promise<A>' is not assignable to type 'Promise<B>'.
5+
Type 'A' is not assignable to type 'B'.
6+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/covariantCallbacks.ts(30,5): error TS2322: Type 'AList1' is not assignable to type 'BList1'.
7+
Types of property 'forEach' are incompatible.
8+
Type '(cb: (item: A) => void) => void' is not assignable to type '(cb: (item: B) => void) => void'.
9+
Types of parameters 'cb' and 'cb' are incompatible.
10+
Types of parameters 'item' and 'item' are incompatible.
11+
Type 'A' is not assignable to type 'B'.
12+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/covariantCallbacks.ts(43,5): error TS2322: Type 'AList2' is not assignable to type 'BList2'.
13+
Types of property 'forEach' are incompatible.
14+
Type '(cb: (item: A) => boolean) => void' is not assignable to type '(cb: (item: A) => void) => void'.
15+
Types of parameters 'cb' and 'cb' are incompatible.
16+
Type 'void' is not assignable to type 'boolean'.
17+
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/covariantCallbacks.ts(56,5): error TS2322: Type 'AList3' is not assignable to type 'BList3'.
18+
Types of property 'forEach' are incompatible.
19+
Type '(cb: (item: A) => void) => void' is not assignable to type '(cb: (item: A, context: any) => void) => void'.
20+
Types of parameters 'cb' and 'cb' are incompatible.
21+
22+
23+
==== tests/cases/conformance/types/typeRelationships/assignmentCompatibility/covariantCallbacks.ts (5 errors) ====
24+
// Test that callback parameters are related covariantly
25+
26+
interface P<T> {
27+
then(cb: (value: T) => void): void;
28+
};
29+
30+
interface A { a: string }
31+
interface B extends A { b: string }
32+
33+
function f1(a: P<A>, b: P<B>) {
34+
a = b;
35+
b = a; // Error
36+
~
37+
!!! error TS2322: Type 'P<A>' is not assignable to type 'P<B>'.
38+
!!! error TS2322: Type 'A' is not assignable to type 'B'.
39+
!!! error TS2322: Property 'b' is missing in type 'A'.
40+
}
41+
42+
function f2(a: Promise<A>, b: Promise<B>) {
43+
a = b;
44+
b = a; // Error
45+
~
46+
!!! error TS2322: Type 'Promise<A>' is not assignable to type 'Promise<B>'.
47+
!!! error TS2322: Type 'A' is not assignable to type 'B'.
48+
}
49+
50+
interface AList1 {
51+
forEach(cb: (item: A) => void): void;
52+
}
53+
54+
interface BList1 {
55+
forEach(cb: (item: B) => void): void;
56+
}
57+
58+
function f11(a: AList1, b: BList1) {
59+
a = b;
60+
b = a; // Error
61+
~
62+
!!! error TS2322: Type 'AList1' is not assignable to type 'BList1'.
63+
!!! error TS2322: Types of property 'forEach' are incompatible.
64+
!!! error TS2322: Type '(cb: (item: A) => void) => void' is not assignable to type '(cb: (item: B) => void) => void'.
65+
!!! error TS2322: Types of parameters 'cb' and 'cb' are incompatible.
66+
!!! error TS2322: Types of parameters 'item' and 'item' are incompatible.
67+
!!! error TS2322: Type 'A' is not assignable to type 'B'.
68+
}
69+
70+
interface AList2 {
71+
forEach(cb: (item: A) => boolean): void;
72+
}
73+
74+
interface BList2 {
75+
forEach(cb: (item: A) => void): void;
76+
}
77+
78+
function f12(a: AList2, b: BList2) {
79+
a = b;
80+
b = a; // Error
81+
~
82+
!!! error TS2322: Type 'AList2' is not assignable to type 'BList2'.
83+
!!! error TS2322: Types of property 'forEach' are incompatible.
84+
!!! error TS2322: Type '(cb: (item: A) => boolean) => void' is not assignable to type '(cb: (item: A) => void) => void'.
85+
!!! error TS2322: Types of parameters 'cb' and 'cb' are incompatible.
86+
!!! error TS2322: Type 'void' is not assignable to type 'boolean'.
87+
}
88+
89+
interface AList3 {
90+
forEach(cb: (item: A) => void): void;
91+
}
92+
93+
interface BList3 {
94+
forEach(cb: (item: A, context: any) => void): void;
95+
}
96+
97+
function f13(a: AList3, b: BList3) {
98+
a = b;
99+
b = a; // Error
100+
~
101+
!!! error TS2322: Type 'AList3' is not assignable to type 'BList3'.
102+
!!! error TS2322: Types of property 'forEach' are incompatible.
103+
!!! error TS2322: Type '(cb: (item: A) => void) => void' is not assignable to type '(cb: (item: A, context: any) => void) => void'.
104+
!!! error TS2322: Types of parameters 'cb' and 'cb' are incompatible.
105+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//// [covariantCallbacks.ts]
2+
// Test that callback parameters are related covariantly
3+
4+
interface P<T> {
5+
then(cb: (value: T) => void): void;
6+
};
7+
8+
interface A { a: string }
9+
interface B extends A { b: string }
10+
11+
function f1(a: P<A>, b: P<B>) {
12+
a = b;
13+
b = a; // Error
14+
}
15+
16+
function f2(a: Promise<A>, b: Promise<B>) {
17+
a = b;
18+
b = a; // Error
19+
}
20+
21+
interface AList1 {
22+
forEach(cb: (item: A) => void): void;
23+
}
24+
25+
interface BList1 {
26+
forEach(cb: (item: B) => void): void;
27+
}
28+
29+
function f11(a: AList1, b: BList1) {
30+
a = b;
31+
b = a; // Error
32+
}
33+
34+
interface AList2 {
35+
forEach(cb: (item: A) => boolean): void;
36+
}
37+
38+
interface BList2 {
39+
forEach(cb: (item: A) => void): void;
40+
}
41+
42+
function f12(a: AList2, b: BList2) {
43+
a = b;
44+
b = a; // Error
45+
}
46+
47+
interface AList3 {
48+
forEach(cb: (item: A) => void): void;
49+
}
50+
51+
interface BList3 {
52+
forEach(cb: (item: A, context: any) => void): void;
53+
}
54+
55+
function f13(a: AList3, b: BList3) {
56+
a = b;
57+
b = a; // Error
58+
}
59+
60+
//// [covariantCallbacks.js]
61+
// Test that callback parameters are related covariantly
62+
;
63+
function f1(a, b) {
64+
a = b;
65+
b = a; // Error
66+
}
67+
function f2(a, b) {
68+
a = b;
69+
b = a; // Error
70+
}
71+
function f11(a, b) {
72+
a = b;
73+
b = a; // Error
74+
}
75+
function f12(a, b) {
76+
a = b;
77+
b = a; // Error
78+
}
79+
function f13(a, b) {
80+
a = b;
81+
b = a; // Error
82+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// @target: es2015
2+
3+
// Test that callback parameters are related covariantly
4+
5+
interface P<T> {
6+
then(cb: (value: T) => void): void;
7+
};
8+
9+
interface A { a: string }
10+
interface B extends A { b: string }
11+
12+
function f1(a: P<A>, b: P<B>) {
13+
a = b;
14+
b = a; // Error
15+
}
16+
17+
function f2(a: Promise<A>, b: Promise<B>) {
18+
a = b;
19+
b = a; // Error
20+
}
21+
22+
interface AList1 {
23+
forEach(cb: (item: A) => void): void;
24+
}
25+
26+
interface BList1 {
27+
forEach(cb: (item: B) => void): void;
28+
}
29+
30+
function f11(a: AList1, b: BList1) {
31+
a = b;
32+
b = a; // Error
33+
}
34+
35+
interface AList2 {
36+
forEach(cb: (item: A) => boolean): void;
37+
}
38+
39+
interface BList2 {
40+
forEach(cb: (item: A) => void): void;
41+
}
42+
43+
function f12(a: AList2, b: BList2) {
44+
a = b;
45+
b = a; // Error
46+
}
47+
48+
interface AList3 {
49+
forEach(cb: (item: A) => void): void;
50+
}
51+
52+
interface BList3 {
53+
forEach(cb: (item: A, context: any) => void): void;
54+
}
55+
56+
function f13(a: AList3, b: BList3) {
57+
a = b;
58+
b = a; // Error
59+
}

0 commit comments

Comments
 (0)