Skip to content

Commit c286684

Browse files
committed
add tests for overloading functions
1 parent cc0a405 commit c286684

File tree

5 files changed

+292
-0
lines changed

5 files changed

+292
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
tests/cases/compiler/strictAssignment4.ts(18,9): error TS2756: Argument of type 'Cat[]' is not assignable to parameter of type 'Animal[]'. Covariant assignment of aliases to non-readonly targets are unsafe.
2+
tests/cases/compiler/strictAssignment4.ts(24,9): error TS2756: Argument of type 'Cat[]' is not assignable to parameter of type 'Dog[]'. Covariant assignment of aliases to non-readonly targets are unsafe.
3+
tests/cases/compiler/strictAssignment4.ts(25,10): error TS2322: Type 'Cat' is not assignable to type 'Dog'.
4+
5+
6+
==== tests/cases/compiler/strictAssignment4.ts (3 errors) ====
7+
module StrictAssignment4 {
8+
class Animal {}
9+
class Cat { purr() {} }
10+
class Dog { bark() {} }
11+
12+
const cats: Cat[] = [new Cat];
13+
14+
function foo(animals: Cat[]): void
15+
function foo(animals: Animal[]): void {
16+
}
17+
foo(cats); // okay, because there's a safe overload
18+
foo([new Cat]); // okay, because there's a safe overload
19+
20+
function oof(animals: Animal[]): void
21+
function oof(animals: Cat[]): void {
22+
}
23+
// TODO: make it so this isn't an error
24+
oof(cats); // okay, because there's a safe overload
25+
~~~~
26+
!!! error TS2756: Argument of type 'Cat[]' is not assignable to parameter of type 'Animal[]'. Covariant assignment of aliases to non-readonly targets are unsafe.
27+
!!! related TS2728 tests/cases/compiler/strictAssignment4.ts:3:17: 'purr' is declared here.
28+
oof([new Cat]); // okay, because there's a safe overload
29+
30+
function bar(animals: Dog[]): void
31+
function bar(animals: Animal[]): void {
32+
}
33+
bar(cats); // error, there's no safe overload
34+
~~~~
35+
!!! error TS2756: Argument of type 'Cat[]' is not assignable to parameter of type 'Dog[]'. Covariant assignment of aliases to non-readonly targets are unsafe.
36+
!!! related TS2728 tests/cases/compiler/strictAssignment4.ts:4:17: 'bark' is declared here.
37+
bar([new Cat]); // error, there's no safe overload
38+
~~~~~~~
39+
!!! error TS2322: Type 'Cat' is not assignable to type 'Dog'.
40+
}
41+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//// [strictAssignment4.ts]
2+
module StrictAssignment4 {
3+
class Animal {}
4+
class Cat { purr() {} }
5+
class Dog { bark() {} }
6+
7+
const cats: Cat[] = [new Cat];
8+
9+
function foo(animals: Cat[]): void
10+
function foo(animals: Animal[]): void {
11+
}
12+
foo(cats); // okay, because there's a safe overload
13+
foo([new Cat]); // okay, because there's a safe overload
14+
15+
function oof(animals: Animal[]): void
16+
function oof(animals: Cat[]): void {
17+
}
18+
// TODO: make it so this isn't an error
19+
oof(cats); // okay, because there's a safe overload
20+
oof([new Cat]); // okay, because there's a safe overload
21+
22+
function bar(animals: Dog[]): void
23+
function bar(animals: Animal[]): void {
24+
}
25+
bar(cats); // error, there's no safe overload
26+
bar([new Cat]); // error, there's no safe overload
27+
}
28+
29+
30+
//// [strictAssignment4.js]
31+
var StrictAssignment4;
32+
(function (StrictAssignment4) {
33+
var Animal = /** @class */ (function () {
34+
function Animal() {
35+
}
36+
return Animal;
37+
}());
38+
var Cat = /** @class */ (function () {
39+
function Cat() {
40+
}
41+
Cat.prototype.purr = function () { };
42+
return Cat;
43+
}());
44+
var Dog = /** @class */ (function () {
45+
function Dog() {
46+
}
47+
Dog.prototype.bark = function () { };
48+
return Dog;
49+
}());
50+
var cats = [new Cat];
51+
function foo(animals) {
52+
}
53+
foo(cats); // okay, because there's a safe overload
54+
foo([new Cat]); // okay, because there's a safe overload
55+
function oof(animals) {
56+
}
57+
// TODO: make it so this isn't an error
58+
oof(cats); // okay, because there's a safe overload
59+
oof([new Cat]); // okay, because there's a safe overload
60+
function bar(animals) {
61+
}
62+
bar(cats); // error, there's no safe overload
63+
bar([new Cat]); // error, there's no safe overload
64+
})(StrictAssignment4 || (StrictAssignment4 = {}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
=== tests/cases/compiler/strictAssignment4.ts ===
2+
module StrictAssignment4 {
3+
>StrictAssignment4 : Symbol(StrictAssignment4, Decl(strictAssignment4.ts, 0, 0))
4+
5+
class Animal {}
6+
>Animal : Symbol(Animal, Decl(strictAssignment4.ts, 0, 26))
7+
8+
class Cat { purr() {} }
9+
>Cat : Symbol(Cat, Decl(strictAssignment4.ts, 1, 19))
10+
>purr : Symbol(Cat.purr, Decl(strictAssignment4.ts, 2, 15))
11+
12+
class Dog { bark() {} }
13+
>Dog : Symbol(Dog, Decl(strictAssignment4.ts, 2, 27))
14+
>bark : Symbol(Dog.bark, Decl(strictAssignment4.ts, 3, 15))
15+
16+
const cats: Cat[] = [new Cat];
17+
>cats : Symbol(cats, Decl(strictAssignment4.ts, 5, 9))
18+
>Cat : Symbol(Cat, Decl(strictAssignment4.ts, 1, 19))
19+
>Cat : Symbol(Cat, Decl(strictAssignment4.ts, 1, 19))
20+
21+
function foo(animals: Cat[]): void
22+
>foo : Symbol(foo, Decl(strictAssignment4.ts, 5, 34), Decl(strictAssignment4.ts, 7, 38))
23+
>animals : Symbol(animals, Decl(strictAssignment4.ts, 7, 17))
24+
>Cat : Symbol(Cat, Decl(strictAssignment4.ts, 1, 19))
25+
26+
function foo(animals: Animal[]): void {
27+
>foo : Symbol(foo, Decl(strictAssignment4.ts, 5, 34), Decl(strictAssignment4.ts, 7, 38))
28+
>animals : Symbol(animals, Decl(strictAssignment4.ts, 8, 17))
29+
>Animal : Symbol(Animal, Decl(strictAssignment4.ts, 0, 26))
30+
}
31+
foo(cats); // okay, because there's a safe overload
32+
>foo : Symbol(foo, Decl(strictAssignment4.ts, 5, 34), Decl(strictAssignment4.ts, 7, 38))
33+
>cats : Symbol(cats, Decl(strictAssignment4.ts, 5, 9))
34+
35+
foo([new Cat]); // okay, because there's a safe overload
36+
>foo : Symbol(foo, Decl(strictAssignment4.ts, 5, 34), Decl(strictAssignment4.ts, 7, 38))
37+
>Cat : Symbol(Cat, Decl(strictAssignment4.ts, 1, 19))
38+
39+
function oof(animals: Animal[]): void
40+
>oof : Symbol(oof, Decl(strictAssignment4.ts, 11, 19), Decl(strictAssignment4.ts, 13, 41))
41+
>animals : Symbol(animals, Decl(strictAssignment4.ts, 13, 17))
42+
>Animal : Symbol(Animal, Decl(strictAssignment4.ts, 0, 26))
43+
44+
function oof(animals: Cat[]): void {
45+
>oof : Symbol(oof, Decl(strictAssignment4.ts, 11, 19), Decl(strictAssignment4.ts, 13, 41))
46+
>animals : Symbol(animals, Decl(strictAssignment4.ts, 14, 17))
47+
>Cat : Symbol(Cat, Decl(strictAssignment4.ts, 1, 19))
48+
}
49+
// TODO: make it so this isn't an error
50+
oof(cats); // okay, because there's a safe overload
51+
>oof : Symbol(oof, Decl(strictAssignment4.ts, 11, 19), Decl(strictAssignment4.ts, 13, 41))
52+
>cats : Symbol(cats, Decl(strictAssignment4.ts, 5, 9))
53+
54+
oof([new Cat]); // okay, because there's a safe overload
55+
>oof : Symbol(oof, Decl(strictAssignment4.ts, 11, 19), Decl(strictAssignment4.ts, 13, 41))
56+
>Cat : Symbol(Cat, Decl(strictAssignment4.ts, 1, 19))
57+
58+
function bar(animals: Dog[]): void
59+
>bar : Symbol(bar, Decl(strictAssignment4.ts, 18, 19), Decl(strictAssignment4.ts, 20, 38))
60+
>animals : Symbol(animals, Decl(strictAssignment4.ts, 20, 17))
61+
>Dog : Symbol(Dog, Decl(strictAssignment4.ts, 2, 27))
62+
63+
function bar(animals: Animal[]): void {
64+
>bar : Symbol(bar, Decl(strictAssignment4.ts, 18, 19), Decl(strictAssignment4.ts, 20, 38))
65+
>animals : Symbol(animals, Decl(strictAssignment4.ts, 21, 17))
66+
>Animal : Symbol(Animal, Decl(strictAssignment4.ts, 0, 26))
67+
}
68+
bar(cats); // error, there's no safe overload
69+
>bar : Symbol(bar, Decl(strictAssignment4.ts, 18, 19), Decl(strictAssignment4.ts, 20, 38))
70+
>cats : Symbol(cats, Decl(strictAssignment4.ts, 5, 9))
71+
72+
bar([new Cat]); // error, there's no safe overload
73+
>bar : Symbol(bar, Decl(strictAssignment4.ts, 18, 19), Decl(strictAssignment4.ts, 20, 38))
74+
>Cat : Symbol(Cat, Decl(strictAssignment4.ts, 1, 19))
75+
}
76+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
=== tests/cases/compiler/strictAssignment4.ts ===
2+
module StrictAssignment4 {
3+
>StrictAssignment4 : typeof StrictAssignment4
4+
5+
class Animal {}
6+
>Animal : Animal
7+
8+
class Cat { purr() {} }
9+
>Cat : Cat
10+
>purr : () => void
11+
12+
class Dog { bark() {} }
13+
>Dog : Dog
14+
>bark : () => void
15+
16+
const cats: Cat[] = [new Cat];
17+
>cats : Cat[]
18+
>[new Cat] : Cat[]
19+
>new Cat : Cat
20+
>Cat : typeof Cat
21+
22+
function foo(animals: Cat[]): void
23+
>foo : (animals: Cat[]) => void
24+
>animals : Cat[]
25+
26+
function foo(animals: Animal[]): void {
27+
>foo : (animals: Cat[]) => void
28+
>animals : Animal[]
29+
}
30+
foo(cats); // okay, because there's a safe overload
31+
>foo(cats) : void
32+
>foo : (animals: Cat[]) => void
33+
>cats : Cat[]
34+
35+
foo([new Cat]); // okay, because there's a safe overload
36+
>foo([new Cat]) : void
37+
>foo : (animals: Cat[]) => void
38+
>[new Cat] : Cat[]
39+
>new Cat : Cat
40+
>Cat : typeof Cat
41+
42+
function oof(animals: Animal[]): void
43+
>oof : (animals: Animal[]) => void
44+
>animals : Animal[]
45+
46+
function oof(animals: Cat[]): void {
47+
>oof : (animals: Animal[]) => void
48+
>animals : Cat[]
49+
}
50+
// TODO: make it so this isn't an error
51+
oof(cats); // okay, because there's a safe overload
52+
>oof(cats) : void
53+
>oof : (animals: Animal[]) => void
54+
>cats : Cat[]
55+
56+
oof([new Cat]); // okay, because there's a safe overload
57+
>oof([new Cat]) : void
58+
>oof : (animals: Animal[]) => void
59+
>[new Cat] : Cat[]
60+
>new Cat : Cat
61+
>Cat : typeof Cat
62+
63+
function bar(animals: Dog[]): void
64+
>bar : (animals: Dog[]) => void
65+
>animals : Dog[]
66+
67+
function bar(animals: Animal[]): void {
68+
>bar : (animals: Dog[]) => void
69+
>animals : Animal[]
70+
}
71+
bar(cats); // error, there's no safe overload
72+
>bar(cats) : void
73+
>bar : (animals: Dog[]) => void
74+
>cats : Cat[]
75+
76+
bar([new Cat]); // error, there's no safe overload
77+
>bar([new Cat]) : void
78+
>bar : (animals: Dog[]) => void
79+
>[new Cat] : Cat[]
80+
>new Cat : Cat
81+
>Cat : typeof Cat
82+
}
83+
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// @strictAssignment: true
2+
3+
module StrictAssignment4 {
4+
class Animal {}
5+
class Cat { purr() {} }
6+
class Dog { bark() {} }
7+
8+
const cats: Cat[] = [new Cat];
9+
10+
function foo(animals: Cat[]): void
11+
function foo(animals: Animal[]): void {
12+
}
13+
foo(cats); // okay, because there's a safe overload
14+
foo([new Cat]); // okay, because there's a safe overload
15+
16+
function oof(animals: Animal[]): void
17+
function oof(animals: Cat[]): void {
18+
}
19+
// TODO: make it so this isn't an error
20+
oof(cats); // okay, because there's a safe overload
21+
oof([new Cat]); // okay, because there's a safe overload
22+
23+
function bar(animals: Dog[]): void
24+
function bar(animals: Animal[]): void {
25+
}
26+
bar(cats); // error, there's no safe overload
27+
bar([new Cat]); // error, there's no safe overload
28+
}

0 commit comments

Comments
 (0)