Steve Staple
Steve Staple

Reputation: 3279

Cannot use 'new' with an expression whose type lacks a call or construct signature.ts

I found this, but it did not help me in this instance: Cannot use 'new' with an expression whose type lacks a call or construct signature

I have a similar problem. The JavaScript has the following:

public clone(data) {
    return new this.constructor({
        ...this.data,
        ...data,
    });
}

This is flagged as an error: Cannot use 'new' with an expression whose type lacks a call or construct signature.ts(2351)

How can I rewrite in TypeScript?

Upvotes: 0

Views: 642

Answers (2)

mbojko
mbojko

Reputation: 14679

Assuming the class is called MyClass,

public clone(data) {
    return new MyClass({
        ...this.data,
        ...data,
    });
}

should work.

Upvotes: 0

Jonas Wilms
Jonas Wilms

Reputation: 138267

I'd explicitly disable typechecking here:

 public clone(data): ThisClass {
   return new (this.constructor as any)({
    ...this.data,
    ...data,
  });
}

Upvotes: 1

Related Questions