0
votes

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?

2
think about what you are trying to achieve. Do you want a new constructor or a new instance of the class? How do you normally create instances of classes?Leon
"I have a similar problem" Similar to what ?Seblor
@Seblor My guess is that this was posted as an answer to another question.Ian Kemp
@Seblor similar to the one described in the link provided!Steve Staple
@Leon - what am I trying to achieve - conversion of existing Javascript code to Typescript.Steve Staple

2 Answers

1
votes

I'd explicitly disable typechecking here:

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

Assuming the class is called MyClass,

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

should work.