Skip to content

Need better error message about incompatible constructor signature #21253

Closed
@fictitious

Description

@fictitious

TypeScript Version: 2.7.0-dev.201xxxxx

Code

class Foo { constructor(a: string) { } }

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

Expected behavior:

Error message should tell the reason why constructor signatures are incompatible.

Actual behavior:

t.ts(3,5): error TS2322: Type 'typeof Foo' is not assignable to type 'new () => Foo'.

This is not particularly helpful, see comments below this answer, especially in comparison to the error message about incompatible methods:

let foo: { foo(a: string): void };
let foo2: { foo(): void };

foo2 = foo;
Type '{ foo(a: string): void; }' is not assignable to type '{ foo(): void; }'.
  Types of property 'foo' are incompatible.
    Type '(a: string) => void' is not assignable to type '() => void'.

So for constructor signature, ideally, the message could be

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'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Error MessagesThe issue relates to error messagingFix AvailableA PR has been opened for this issueFixedA PR has been merged for this issueGood First IssueWell scoped, documented and has the green lightHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions