Reputation: 3279
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
Reputation: 14679
Assuming the class is called MyClass,
public clone(data) {
return new MyClass({
...this.data,
...data,
});
}
should work.
Upvotes: 0
Reputation: 138267
I'd explicitly disable typechecking here:
public clone(data): ThisClass {
return new (this.constructor as any)({
...this.data,
...data,
});
}
Upvotes: 1