Skip to content

Commit b2bae85

Browse files
authored
Merge pull request #27609 from Microsoft/betterStaticError
Report the errors for static incompatibility only if instance types are assignable
2 parents 3e91652 + f30e73f commit b2bae85

6 files changed

+119
-2
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26111,8 +26111,11 @@ namespace ts {
2611126111
if (!checkTypeAssignableTo(typeWithThis, baseWithThis, /*errorNode*/ undefined)) {
2611226112
issueMemberSpecificError(node, typeWithThis, baseWithThis, Diagnostics.Class_0_incorrectly_extends_base_class_1);
2611326113
}
26114-
checkTypeAssignableTo(staticType, getTypeWithoutSignatures(staticBaseType), node.name || node,
26115-
Diagnostics.Class_static_side_0_incorrectly_extends_base_class_static_side_1);
26114+
else {
26115+
// Report static side error only when instance type is assignable
26116+
checkTypeAssignableTo(staticType, getTypeWithoutSignatures(staticBaseType), node.name || node,
26117+
Diagnostics.Class_static_side_0_incorrectly_extends_base_class_static_side_1);
26118+
}
2611626119
if (baseConstructorType.flags & TypeFlags.TypeVariable && !isMixinConstructorType(staticType)) {
2611726120
error(node.name || node, Diagnostics.A_mixin_class_must_have_a_constructor_with_a_single_rest_parameter_of_type_any);
2611826121
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
tests/cases/compiler/staticMismatchBecauseOfPrototype.ts(10,5): error TS2416: Property 'n' in type 'B' is not assignable to the same property in base type 'A'.
2+
Type 'string' is not assignable to type 'number'.
3+
4+
5+
==== tests/cases/compiler/staticMismatchBecauseOfPrototype.ts (1 errors) ====
6+
interface A {
7+
n: number;
8+
}
9+
declare var A: {
10+
prototype: A;
11+
new(): A;
12+
};
13+
14+
class B extends A {
15+
n = "";
16+
~
17+
!!! error TS2416: Property 'n' in type 'B' is not assignable to the same property in base type 'A'.
18+
!!! error TS2416: Type 'string' is not assignable to type 'number'.
19+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [staticMismatchBecauseOfPrototype.ts]
2+
interface A {
3+
n: number;
4+
}
5+
declare var A: {
6+
prototype: A;
7+
new(): A;
8+
};
9+
10+
class B extends A {
11+
n = "";
12+
}
13+
14+
//// [staticMismatchBecauseOfPrototype.js]
15+
var __extends = (this && this.__extends) || (function () {
16+
var extendStatics = function (d, b) {
17+
extendStatics = Object.setPrototypeOf ||
18+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
19+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
20+
return extendStatics(d, b);
21+
};
22+
return function (d, b) {
23+
extendStatics(d, b);
24+
function __() { this.constructor = d; }
25+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
26+
};
27+
})();
28+
var B = /** @class */ (function (_super) {
29+
__extends(B, _super);
30+
function B() {
31+
var _this = _super !== null && _super.apply(this, arguments) || this;
32+
_this.n = "";
33+
return _this;
34+
}
35+
return B;
36+
}(A));
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
=== tests/cases/compiler/staticMismatchBecauseOfPrototype.ts ===
2+
interface A {
3+
>A : Symbol(A, Decl(staticMismatchBecauseOfPrototype.ts, 0, 0), Decl(staticMismatchBecauseOfPrototype.ts, 3, 11))
4+
5+
n: number;
6+
>n : Symbol(A.n, Decl(staticMismatchBecauseOfPrototype.ts, 0, 13))
7+
}
8+
declare var A: {
9+
>A : Symbol(A, Decl(staticMismatchBecauseOfPrototype.ts, 0, 0), Decl(staticMismatchBecauseOfPrototype.ts, 3, 11))
10+
11+
prototype: A;
12+
>prototype : Symbol(prototype, Decl(staticMismatchBecauseOfPrototype.ts, 3, 16))
13+
>A : Symbol(A, Decl(staticMismatchBecauseOfPrototype.ts, 0, 0), Decl(staticMismatchBecauseOfPrototype.ts, 3, 11))
14+
15+
new(): A;
16+
>A : Symbol(A, Decl(staticMismatchBecauseOfPrototype.ts, 0, 0), Decl(staticMismatchBecauseOfPrototype.ts, 3, 11))
17+
18+
};
19+
20+
class B extends A {
21+
>B : Symbol(B, Decl(staticMismatchBecauseOfPrototype.ts, 6, 2))
22+
>A : Symbol(A, Decl(staticMismatchBecauseOfPrototype.ts, 0, 0), Decl(staticMismatchBecauseOfPrototype.ts, 3, 11))
23+
24+
n = "";
25+
>n : Symbol(B.n, Decl(staticMismatchBecauseOfPrototype.ts, 8, 19))
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/compiler/staticMismatchBecauseOfPrototype.ts ===
2+
interface A {
3+
n: number;
4+
>n : number
5+
}
6+
declare var A: {
7+
>A : { new (): A; prototype: A; }
8+
9+
prototype: A;
10+
>prototype : A
11+
12+
new(): A;
13+
};
14+
15+
class B extends A {
16+
>B : B
17+
>A : A
18+
19+
n = "";
20+
>n : string
21+
>"" : ""
22+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
interface A {
2+
n: number;
3+
}
4+
declare var A: {
5+
prototype: A;
6+
new(): A;
7+
};
8+
9+
class B extends A {
10+
n = "";
11+
}

0 commit comments

Comments
 (0)