Skip to content

Commit

Permalink
Add more information in case of incompatible constructor signature mi…
Browse files Browse the repository at this point in the history
  • Loading branch information
amaksimovich committed Mar 13, 2019
1 parent 1463b32 commit c3e0413
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13232,6 +13232,14 @@ namespace ts {
// to the quadratic nature of the logic below.
const eraseGenerics = relation === comparableRelation || !!compilerOptions.noStrictGenericChecks;
result = signatureRelatedTo(sourceSignatures[0], targetSignatures[0], eraseGenerics, reportErrors);
if (result === Ternary.False && reportErrors && targetSignatures[0].declaration && isConstructSignatureDeclaration(targetSignatures[0].declaration)
&& getObjectFlags(source) & getObjectFlags(target)) {
const flags = TypeFormatFlags.WriteArrowStyleSignature;
const sourceSignature = signatureToString(sourceSignatures[0], /*enclosingDeclaration*/ undefined, flags, kind);
const targetSignature = signatureToString(targetSignatures[0], /*enclosingDeclaration*/ undefined, flags, kind);
reportError(Diagnostics.Type_0_is_not_assignable_to_type_1, sourceSignature, targetSignature);
reportError(Diagnostics.Types_of_constructor_signature_are_incompatible);
}
}
else {
outer: for (const t of targetSignatures) {
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2601,6 +2601,10 @@
"category": "Error",
"code": 2753
},
"Types of constructor signature are incompatible.": {
"category": "Error",
"code": 2754
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
tests/cases/compiler/assigningIncompatibleConstructor.ts(2,5): error TS2322: Type 'typeof Foo' is not assignable to type 'new () => Foo'.
Types of constructor signature are incompatible.
Type 'new (a: string) => Foo' is not assignable to type 'new () => Foo'.


==== tests/cases/compiler/assigningIncompatibleConstructor.ts (1 errors) ====
class Foo { constructor(a: string) { } }
let FooConstructor: { new(): Foo } = Foo;
~~~~~~~~~~~~~~
!!! error TS2322: Type 'typeof Foo' is not assignable to type 'new () => Foo'.
!!! error TS2322: Types of constructor signature are incompatible.
!!! error TS2322: Type 'new (a: string) => Foo' is not assignable to type 'new () => Foo'.

12 changes: 12 additions & 0 deletions tests/baselines/reference/assigningIncompatibleConstructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//// [assigningIncompatibleConstructor.ts]
class Foo { constructor(a: string) { } }
let FooConstructor: { new(): Foo } = Foo;


//// [assigningIncompatibleConstructor.js]
var Foo = /** @class */ (function () {
function Foo(a) {
}
return Foo;
}());
var FooConstructor = Foo;
10 changes: 10 additions & 0 deletions tests/baselines/reference/assigningIncompatibleConstructor.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
=== tests/cases/compiler/assigningIncompatibleConstructor.ts ===
class Foo { constructor(a: string) { } }
>Foo : Symbol(Foo, Decl(assigningIncompatibleConstructor.ts, 0, 0))
>a : Symbol(a, Decl(assigningIncompatibleConstructor.ts, 0, 24))

let FooConstructor: { new(): Foo } = Foo;
>FooConstructor : Symbol(FooConstructor, Decl(assigningIncompatibleConstructor.ts, 1, 3))
>Foo : Symbol(Foo, Decl(assigningIncompatibleConstructor.ts, 0, 0))
>Foo : Symbol(Foo, Decl(assigningIncompatibleConstructor.ts, 0, 0))

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=== tests/cases/compiler/assigningIncompatibleConstructor.ts ===
class Foo { constructor(a: string) { } }
>Foo : Foo
>a : string

let FooConstructor: { new(): Foo } = Foo;
>FooConstructor : new () => Foo
>Foo : typeof Foo

2 changes: 2 additions & 0 deletions tests/cases/compiler/assigningIncompatibleConstructor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Foo { constructor(a: string) { } }
let FooConstructor: { new(): Foo } = Foo;

0 comments on commit c3e0413

Please sign in to comment.