File tree Expand file tree Collapse file tree 4 files changed +41
-21
lines changed Expand file tree Collapse file tree 4 files changed +41
-21
lines changed Original file line number Diff line number Diff line change @@ -6609,17 +6609,17 @@ export class Compiler extends DiagnosticEmitter {
6609
6609
for ( let i = 0 , k = overrideInstances . length ; i < k ; ++ i ) {
6610
6610
let overrideInstance = overrideInstances [ i ] ;
6611
6611
if ( ! overrideInstance . is ( CommonFlags . Compiled ) ) continue ; // errored
6612
- let overrideType = overrideInstance . type ;
6613
- let originalType = instance . type ;
6614
- if ( ! overrideType . isAssignableTo ( originalType ) ) {
6612
+ let overrideSignature = overrideInstance . signature ;
6613
+ let originalSignature = instance . signature ;
6614
+ if ( ! overrideSignature . isAssignableTo ( originalSignature , true ) ) {
6615
6615
this . error (
6616
6616
DiagnosticCode . Type_0_is_not_assignable_to_type_1 ,
6617
- overrideInstance . identifierNode . range , overrideType . toString ( ) , originalType . toString ( )
6617
+ overrideInstance . identifierNode . range ,
6618
+ overrideSignature . toString ( ) , originalSignature . toString ( )
6618
6619
) ;
6619
6620
continue ;
6620
6621
}
6621
6622
// TODO: additional optional parameters are not permitted by `isAssignableTo` yet
6622
- let overrideSignature = overrideInstance . signature ;
6623
6623
let overrideParameterTypes = overrideSignature . parameterTypes ;
6624
6624
let overrideNumParameters = overrideParameterTypes . length ;
6625
6625
let paramExprs = new Array < ExpressionRef > ( 1 + overrideNumParameters ) ;
Original file line number Diff line number Diff line change @@ -1051,22 +1051,14 @@ export class Signature {
1051
1051
isAssignableTo ( target : Signature , checkCompatibleOverride : bool = false ) : bool {
1052
1052
let thisThisType = this . thisType ;
1053
1053
let targetThisType = target . thisType ;
1054
- if ( checkCompatibleOverride ) {
1055
- // check kind of `this` type
1056
- if ( thisThisType ) {
1057
- if ( ! targetThisType || ! thisThisType . canExtendOrImplement ( targetThisType ) ) {
1058
- return false ;
1059
- }
1060
- } else if ( targetThisType ) {
1061
- return false ;
1062
- }
1063
- } else {
1064
- // check `this` type (invariant)
1065
- if ( thisThisType ) {
1066
- if ( targetThisType != targetThisType ) return false ;
1067
- } else if ( targetThisType ) {
1068
- return false ;
1069
- }
1054
+ if ( thisThisType && targetThisType ) {
1055
+ let compatibleThisType = checkCompatibleOverride
1056
+ ? thisThisType . canExtendOrImplement ( targetThisType )
1057
+ : targetThisType . isAssignableTo ( thisThisType ) ;
1058
+
1059
+ if ( ! compatibleThisType ) return false ;
1060
+ } else if ( thisThisType || targetThisType ) {
1061
+ return false ;
1070
1062
}
1071
1063
1072
1064
// check rest parameter
Original file line number Diff line number Diff line change
1
+ {
2
+ "stderr" : [
3
+ " TS2322: Type '() => void' is not assignable to type '(this: class-method-as-parameter/A) => void'." ,
4
+ " TS2322: Type '(this: class-method-as-parameter/B) => void' is not assignable to type '(this: class-method-as-parameter/A) => void'." ,
5
+ " EOF"
6
+ ]
7
+ }
Original file line number Diff line number Diff line change
1
+ class A {
2
+ foo ( ) : void { }
3
+ }
4
+
5
+ class B extends A {
6
+ foo ( ) : void { }
7
+ }
8
+
9
+ function foo ( ) : void { }
10
+
11
+ function consumeA ( callback : ( this : A ) => void ) : void { }
12
+ function consumeB ( callback : ( this : B ) => void ) : void { }
13
+
14
+ const a = new A ( ) ;
15
+ const b = new B ( ) ;
16
+
17
+ consumeB ( a . foo ) ; // shouldn't error
18
+ consumeA ( foo ) ; // should error
19
+ consumeA ( b . foo ) ; // should error
20
+
21
+ ERROR ( "EOF" ) ;
You can’t perform that action at this time.
0 commit comments