Skip to content

Commit 7fcf519

Browse files
committed
Add tests
1 parent c8d5260 commit 7fcf519

File tree

5 files changed

+267
-3
lines changed

5 files changed

+267
-3
lines changed

tests/baselines/reference/strictFunctionTypesErrors.errors.txt

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,17 @@ tests/cases/compiler/strictFunctionTypesErrors.ts(134,1): error TS2322: Type '(f
9494
Types of parameters 'f' and 'f' are incompatible.
9595
Types of parameters 'x' and 'x' are incompatible.
9696
Type 'Animal' is not assignable to type 'Dog'.
97+
tests/cases/compiler/strictFunctionTypesErrors.ts(147,5): error TS2322: Type '(cb: (x: Animal) => Animal) => void' is not assignable to type '(cb: (x: Dog) => Animal) => void'.
98+
Types of parameters 'cb' and 'cb' are incompatible.
99+
Types of parameters 'x' and 'x' are incompatible.
100+
Type 'Animal' is not assignable to type 'Dog'.
101+
tests/cases/compiler/strictFunctionTypesErrors.ts(155,5): error TS2322: Type '(cb: (x: Animal) => Animal) => void' is not assignable to type '(cb: (x: Dog) => Animal) => void'.
102+
Types of parameters 'cb' and 'cb' are incompatible.
103+
Types of parameters 'x' and 'x' are incompatible.
104+
Type 'Animal' is not assignable to type 'Dog'.
97105

98106

99-
==== tests/cases/compiler/strictFunctionTypesErrors.ts (33 errors) ====
107+
==== tests/cases/compiler/strictFunctionTypesErrors.ts (35 errors) ====
100108
export {}
101109

102110

@@ -360,4 +368,35 @@ tests/cases/compiler/strictFunctionTypesErrors.ts(134,1): error TS2322: Type '(f
360368
!!! error TS2322: Types of parameters 'f' and 'f' are incompatible.
361369
!!! error TS2322: Types of parameters 'x' and 'x' are incompatible.
362370
!!! error TS2322: Type 'Animal' is not assignable to type 'Dog'.
363-
371+
372+
// Verify that callback parameters aren't loosely checked when types
373+
// originate in method declarations
374+
375+
namespace n1 {
376+
class Foo {
377+
static f1(x: Animal): Animal { throw "wat"; }
378+
static f2(x: Dog): Animal { throw "wat"; };
379+
}
380+
declare let f1: (cb: typeof Foo.f1) => void;
381+
declare let f2: (cb: typeof Foo.f2) => void;
382+
f1 = f2;
383+
f2 = f1; // Error
384+
~~
385+
!!! error TS2322: Type '(cb: (x: Animal) => Animal) => void' is not assignable to type '(cb: (x: Dog) => Animal) => void'.
386+
!!! error TS2322: Types of parameters 'cb' and 'cb' are incompatible.
387+
!!! error TS2322: Types of parameters 'x' and 'x' are incompatible.
388+
!!! error TS2322: Type 'Animal' is not assignable to type 'Dog'.
389+
}
390+
391+
namespace n2 {
392+
type BivariantHack<Input, Output> = { foo(x: Input): Output }["foo"];
393+
declare let f1: (cb: BivariantHack<Animal, Animal>) => void;
394+
declare let f2: (cb: BivariantHack<Dog, Animal>) => void;
395+
f1 = f2;
396+
f2 = f1; // Error
397+
~~
398+
!!! error TS2322: Type '(cb: (x: Animal) => Animal) => void' is not assignable to type '(cb: (x: Dog) => Animal) => void'.
399+
!!! error TS2322: Types of parameters 'cb' and 'cb' are incompatible.
400+
!!! error TS2322: Types of parameters 'x' and 'x' are incompatible.
401+
!!! error TS2322: Type 'Animal' is not assignable to type 'Dog'.
402+
}

tests/baselines/reference/strictFunctionTypesErrors.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,28 @@ declare let fc1: (f: (x: Animal) => Animal) => void;
133133
declare let fc2: (f: (x: Dog) => Dog) => void;
134134
fc1 = fc2; // Error
135135
fc2 = fc1; // Error
136-
136+
137+
// Verify that callback parameters aren't loosely checked when types
138+
// originate in method declarations
139+
140+
namespace n1 {
141+
class Foo {
142+
static f1(x: Animal): Animal { throw "wat"; }
143+
static f2(x: Dog): Animal { throw "wat"; };
144+
}
145+
declare let f1: (cb: typeof Foo.f1) => void;
146+
declare let f2: (cb: typeof Foo.f2) => void;
147+
f1 = f2;
148+
f2 = f1; // Error
149+
}
150+
151+
namespace n2 {
152+
type BivariantHack<Input, Output> = { foo(x: Input): Output }["foo"];
153+
declare let f1: (cb: BivariantHack<Animal, Animal>) => void;
154+
declare let f2: (cb: BivariantHack<Dog, Animal>) => void;
155+
f1 = f2;
156+
f2 = f1; // Error
157+
}
137158

138159
//// [strictFunctionTypesErrors.js]
139160
"use strict";
@@ -195,3 +216,23 @@ animalCrate = dogCrate; // Error
195216
dogCrate = animalCrate; // Error
196217
fc1 = fc2; // Error
197218
fc2 = fc1; // Error
219+
// Verify that callback parameters aren't loosely checked when types
220+
// originate in method declarations
221+
var n1;
222+
(function (n1) {
223+
var Foo = /** @class */ (function () {
224+
function Foo() {
225+
}
226+
Foo.f1 = function (x) { throw "wat"; };
227+
Foo.f2 = function (x) { throw "wat"; };
228+
;
229+
return Foo;
230+
}());
231+
f1 = f2;
232+
f2 = f1; // Error
233+
})(n1 || (n1 = {}));
234+
var n2;
235+
(function (n2) {
236+
f1 = f2;
237+
f2 = f1; // Error
238+
})(n2 || (n2 = {}));

tests/baselines/reference/strictFunctionTypesErrors.symbols

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,3 +424,81 @@ fc2 = fc1; // Error
424424
>fc2 : Symbol(fc2, Decl(strictFunctionTypesErrors.ts, 131, 11))
425425
>fc1 : Symbol(fc1, Decl(strictFunctionTypesErrors.ts, 130, 11))
426426

427+
// Verify that callback parameters aren't loosely checked when types
428+
// originate in method declarations
429+
430+
namespace n1 {
431+
>n1 : Symbol(n1, Decl(strictFunctionTypesErrors.ts, 133, 10))
432+
433+
class Foo {
434+
>Foo : Symbol(Foo, Decl(strictFunctionTypesErrors.ts, 138, 14))
435+
436+
static f1(x: Animal): Animal { throw "wat"; }
437+
>f1 : Symbol(Foo.f1, Decl(strictFunctionTypesErrors.ts, 139, 15))
438+
>x : Symbol(x, Decl(strictFunctionTypesErrors.ts, 140, 18))
439+
>Animal : Symbol(Animal, Decl(strictFunctionTypesErrors.ts, 87, 8))
440+
>Animal : Symbol(Animal, Decl(strictFunctionTypesErrors.ts, 87, 8))
441+
442+
static f2(x: Dog): Animal { throw "wat"; };
443+
>f2 : Symbol(Foo.f2, Decl(strictFunctionTypesErrors.ts, 140, 53))
444+
>x : Symbol(x, Decl(strictFunctionTypesErrors.ts, 141, 18))
445+
>Dog : Symbol(Dog, Decl(strictFunctionTypesErrors.ts, 89, 33))
446+
>Animal : Symbol(Animal, Decl(strictFunctionTypesErrors.ts, 87, 8))
447+
}
448+
declare let f1: (cb: typeof Foo.f1) => void;
449+
>f1 : Symbol(f1, Decl(strictFunctionTypesErrors.ts, 143, 15))
450+
>cb : Symbol(cb, Decl(strictFunctionTypesErrors.ts, 143, 21))
451+
>Foo.f1 : Symbol(Foo.f1, Decl(strictFunctionTypesErrors.ts, 139, 15))
452+
>Foo : Symbol(Foo, Decl(strictFunctionTypesErrors.ts, 138, 14))
453+
>f1 : Symbol(Foo.f1, Decl(strictFunctionTypesErrors.ts, 139, 15))
454+
455+
declare let f2: (cb: typeof Foo.f2) => void;
456+
>f2 : Symbol(f2, Decl(strictFunctionTypesErrors.ts, 144, 15))
457+
>cb : Symbol(cb, Decl(strictFunctionTypesErrors.ts, 144, 21))
458+
>Foo.f2 : Symbol(Foo.f2, Decl(strictFunctionTypesErrors.ts, 140, 53))
459+
>Foo : Symbol(Foo, Decl(strictFunctionTypesErrors.ts, 138, 14))
460+
>f2 : Symbol(Foo.f2, Decl(strictFunctionTypesErrors.ts, 140, 53))
461+
462+
f1 = f2;
463+
>f1 : Symbol(f1, Decl(strictFunctionTypesErrors.ts, 143, 15))
464+
>f2 : Symbol(f2, Decl(strictFunctionTypesErrors.ts, 144, 15))
465+
466+
f2 = f1; // Error
467+
>f2 : Symbol(f2, Decl(strictFunctionTypesErrors.ts, 144, 15))
468+
>f1 : Symbol(f1, Decl(strictFunctionTypesErrors.ts, 143, 15))
469+
}
470+
471+
namespace n2 {
472+
>n2 : Symbol(n2, Decl(strictFunctionTypesErrors.ts, 147, 1))
473+
474+
type BivariantHack<Input, Output> = { foo(x: Input): Output }["foo"];
475+
>BivariantHack : Symbol(BivariantHack, Decl(strictFunctionTypesErrors.ts, 149, 14))
476+
>Input : Symbol(Input, Decl(strictFunctionTypesErrors.ts, 150, 23))
477+
>Output : Symbol(Output, Decl(strictFunctionTypesErrors.ts, 150, 29))
478+
>foo : Symbol(foo, Decl(strictFunctionTypesErrors.ts, 150, 41))
479+
>x : Symbol(x, Decl(strictFunctionTypesErrors.ts, 150, 46))
480+
>Input : Symbol(Input, Decl(strictFunctionTypesErrors.ts, 150, 23))
481+
>Output : Symbol(Output, Decl(strictFunctionTypesErrors.ts, 150, 29))
482+
483+
declare let f1: (cb: BivariantHack<Animal, Animal>) => void;
484+
>f1 : Symbol(f1, Decl(strictFunctionTypesErrors.ts, 151, 15))
485+
>cb : Symbol(cb, Decl(strictFunctionTypesErrors.ts, 151, 21))
486+
>BivariantHack : Symbol(BivariantHack, Decl(strictFunctionTypesErrors.ts, 149, 14))
487+
>Animal : Symbol(Animal, Decl(strictFunctionTypesErrors.ts, 87, 8))
488+
>Animal : Symbol(Animal, Decl(strictFunctionTypesErrors.ts, 87, 8))
489+
490+
declare let f2: (cb: BivariantHack<Dog, Animal>) => void;
491+
>f2 : Symbol(f2, Decl(strictFunctionTypesErrors.ts, 152, 15))
492+
>cb : Symbol(cb, Decl(strictFunctionTypesErrors.ts, 152, 21))
493+
>BivariantHack : Symbol(BivariantHack, Decl(strictFunctionTypesErrors.ts, 149, 14))
494+
>Dog : Symbol(Dog, Decl(strictFunctionTypesErrors.ts, 89, 33))
495+
>Animal : Symbol(Animal, Decl(strictFunctionTypesErrors.ts, 87, 8))
496+
497+
f1 = f2;
498+
>f1 : Symbol(f1, Decl(strictFunctionTypesErrors.ts, 151, 15))
499+
>f2 : Symbol(f2, Decl(strictFunctionTypesErrors.ts, 152, 15))
500+
501+
f2 = f1; // Error
502+
>f2 : Symbol(f2, Decl(strictFunctionTypesErrors.ts, 152, 15))
503+
>f1 : Symbol(f1, Decl(strictFunctionTypesErrors.ts, 151, 15))
504+
}

tests/baselines/reference/strictFunctionTypesErrors.types

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,87 @@ fc2 = fc1; // Error
480480
>fc2 : (f: (x: Dog) => Dog) => void
481481
>fc1 : (f: (x: Animal) => Animal) => void
482482

483+
// Verify that callback parameters aren't loosely checked when types
484+
// originate in method declarations
485+
486+
namespace n1 {
487+
>n1 : typeof n1
488+
489+
class Foo {
490+
>Foo : Foo
491+
492+
static f1(x: Animal): Animal { throw "wat"; }
493+
>f1 : (x: Animal) => Animal
494+
>x : Animal
495+
>Animal : Animal
496+
>Animal : Animal
497+
>"wat" : "wat"
498+
499+
static f2(x: Dog): Animal { throw "wat"; };
500+
>f2 : (x: Dog) => Animal
501+
>x : Dog
502+
>Dog : Dog
503+
>Animal : Animal
504+
>"wat" : "wat"
505+
}
506+
declare let f1: (cb: typeof Foo.f1) => void;
507+
>f1 : (cb: (x: Animal) => Animal) => void
508+
>cb : (x: Animal) => Animal
509+
>Foo.f1 : (x: Animal) => Animal
510+
>Foo : typeof Foo
511+
>f1 : (x: Animal) => Animal
512+
513+
declare let f2: (cb: typeof Foo.f2) => void;
514+
>f2 : (cb: (x: Dog) => Animal) => void
515+
>cb : (x: Dog) => Animal
516+
>Foo.f2 : (x: Dog) => Animal
517+
>Foo : typeof Foo
518+
>f2 : (x: Dog) => Animal
519+
520+
f1 = f2;
521+
>f1 = f2 : (cb: (x: Dog) => Animal) => void
522+
>f1 : (cb: (x: Animal) => Animal) => void
523+
>f2 : (cb: (x: Dog) => Animal) => void
524+
525+
f2 = f1; // Error
526+
>f2 = f1 : (cb: (x: Animal) => Animal) => void
527+
>f2 : (cb: (x: Dog) => Animal) => void
528+
>f1 : (cb: (x: Animal) => Animal) => void
529+
}
530+
531+
namespace n2 {
532+
>n2 : typeof n2
533+
534+
type BivariantHack<Input, Output> = { foo(x: Input): Output }["foo"];
535+
>BivariantHack : (x: Input) => Output
536+
>Input : Input
537+
>Output : Output
538+
>foo : (x: Input) => Output
539+
>x : Input
540+
>Input : Input
541+
>Output : Output
542+
543+
declare let f1: (cb: BivariantHack<Animal, Animal>) => void;
544+
>f1 : (cb: (x: Animal) => Animal) => void
545+
>cb : (x: Animal) => Animal
546+
>BivariantHack : (x: Input) => Output
547+
>Animal : Animal
548+
>Animal : Animal
549+
550+
declare let f2: (cb: BivariantHack<Dog, Animal>) => void;
551+
>f2 : (cb: (x: Dog) => Animal) => void
552+
>cb : (x: Dog) => Animal
553+
>BivariantHack : (x: Input) => Output
554+
>Dog : Dog
555+
>Animal : Animal
556+
557+
f1 = f2;
558+
>f1 = f2 : (cb: (x: Dog) => Animal) => void
559+
>f1 : (cb: (x: Animal) => Animal) => void
560+
>f2 : (cb: (x: Dog) => Animal) => void
561+
562+
f2 = f1; // Error
563+
>f2 = f1 : (cb: (x: Animal) => Animal) => void
564+
>f2 : (cb: (x: Dog) => Animal) => void
565+
>f1 : (cb: (x: Animal) => Animal) => void
566+
}

tests/cases/compiler/strictFunctionTypesErrors.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,25 @@ declare let fc1: (f: (x: Animal) => Animal) => void;
133133
declare let fc2: (f: (x: Dog) => Dog) => void;
134134
fc1 = fc2; // Error
135135
fc2 = fc1; // Error
136+
137+
// Verify that callback parameters aren't loosely checked when types
138+
// originate in method declarations
139+
140+
namespace n1 {
141+
class Foo {
142+
static f1(x: Animal): Animal { throw "wat"; }
143+
static f2(x: Dog): Animal { throw "wat"; };
144+
}
145+
declare let f1: (cb: typeof Foo.f1) => void;
146+
declare let f2: (cb: typeof Foo.f2) => void;
147+
f1 = f2;
148+
f2 = f1; // Error
149+
}
150+
151+
namespace n2 {
152+
type BivariantHack<Input, Output> = { foo(x: Input): Output }["foo"];
153+
declare let f1: (cb: BivariantHack<Animal, Animal>) => void;
154+
declare let f2: (cb: BivariantHack<Dog, Animal>) => void;
155+
f1 = f2;
156+
f2 = f1; // Error
157+
}

0 commit comments

Comments
 (0)