Skip to content

Commit

Permalink
Merge pull request microsoft#27609 from Microsoft/betterStaticError
Browse files Browse the repository at this point in the history
Report the errors for static incompatibility only if instance types are assignable
  • Loading branch information
sheetalkamat authored Oct 10, 2018
2 parents 3e91652 + f30e73f commit b2bae85
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26111,8 +26111,11 @@ namespace ts {
if (!checkTypeAssignableTo(typeWithThis, baseWithThis, /*errorNode*/ undefined)) {
issueMemberSpecificError(node, typeWithThis, baseWithThis, Diagnostics.Class_0_incorrectly_extends_base_class_1);
}
checkTypeAssignableTo(staticType, getTypeWithoutSignatures(staticBaseType), node.name || node,
Diagnostics.Class_static_side_0_incorrectly_extends_base_class_static_side_1);
else {
// Report static side error only when instance type is assignable
checkTypeAssignableTo(staticType, getTypeWithoutSignatures(staticBaseType), node.name || node,
Diagnostics.Class_static_side_0_incorrectly_extends_base_class_static_side_1);
}
if (baseConstructorType.flags & TypeFlags.TypeVariable && !isMixinConstructorType(staticType)) {
error(node.name || node, Diagnostics.A_mixin_class_must_have_a_constructor_with_a_single_rest_parameter_of_type_any);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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'.
Type 'string' is not assignable to type 'number'.


==== tests/cases/compiler/staticMismatchBecauseOfPrototype.ts (1 errors) ====
interface A {
n: number;
}
declare var A: {
prototype: A;
new(): A;
};

class B extends A {
n = "";
~
!!! error TS2416: Property 'n' in type 'B' is not assignable to the same property in base type 'A'.
!!! error TS2416: Type 'string' is not assignable to type 'number'.
}
36 changes: 36 additions & 0 deletions tests/baselines/reference/staticMismatchBecauseOfPrototype.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//// [staticMismatchBecauseOfPrototype.ts]
interface A {
n: number;
}
declare var A: {
prototype: A;
new(): A;
};

class B extends A {
n = "";
}

//// [staticMismatchBecauseOfPrototype.js]
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var B = /** @class */ (function (_super) {
__extends(B, _super);
function B() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.n = "";
return _this;
}
return B;
}(A));
26 changes: 26 additions & 0 deletions tests/baselines/reference/staticMismatchBecauseOfPrototype.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
=== tests/cases/compiler/staticMismatchBecauseOfPrototype.ts ===
interface A {
>A : Symbol(A, Decl(staticMismatchBecauseOfPrototype.ts, 0, 0), Decl(staticMismatchBecauseOfPrototype.ts, 3, 11))

n: number;
>n : Symbol(A.n, Decl(staticMismatchBecauseOfPrototype.ts, 0, 13))
}
declare var A: {
>A : Symbol(A, Decl(staticMismatchBecauseOfPrototype.ts, 0, 0), Decl(staticMismatchBecauseOfPrototype.ts, 3, 11))

prototype: A;
>prototype : Symbol(prototype, Decl(staticMismatchBecauseOfPrototype.ts, 3, 16))
>A : Symbol(A, Decl(staticMismatchBecauseOfPrototype.ts, 0, 0), Decl(staticMismatchBecauseOfPrototype.ts, 3, 11))

new(): A;
>A : Symbol(A, Decl(staticMismatchBecauseOfPrototype.ts, 0, 0), Decl(staticMismatchBecauseOfPrototype.ts, 3, 11))

};

class B extends A {
>B : Symbol(B, Decl(staticMismatchBecauseOfPrototype.ts, 6, 2))
>A : Symbol(A, Decl(staticMismatchBecauseOfPrototype.ts, 0, 0), Decl(staticMismatchBecauseOfPrototype.ts, 3, 11))

n = "";
>n : Symbol(B.n, Decl(staticMismatchBecauseOfPrototype.ts, 8, 19))
}
22 changes: 22 additions & 0 deletions tests/baselines/reference/staticMismatchBecauseOfPrototype.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/compiler/staticMismatchBecauseOfPrototype.ts ===
interface A {
n: number;
>n : number
}
declare var A: {
>A : { new (): A; prototype: A; }

prototype: A;
>prototype : A

new(): A;
};

class B extends A {
>B : B
>A : A

n = "";
>n : string
>"" : ""
}
11 changes: 11 additions & 0 deletions tests/cases/compiler/staticMismatchBecauseOfPrototype.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
interface A {
n: number;
}
declare var A: {
prototype: A;
new(): A;
};

class B extends A {
n = "";
}

0 comments on commit b2bae85

Please sign in to comment.