I'm currently trying to create a decorator to mix new methods into a React component. I've gotten the desired result via the following:
function testDecorator(target) {
const testMethod = function testMethod() {
return 'successfully mixed testMethod into component';
};
target.prototype.testMethod = testMethod;
}
@testDecorator
export class TestComponent extends React.Component<{}, {}> {
// ...
render() {
console.log('this.testMethod', this.testMethod());
// outputs "successfully mixed testMethod into component"
}
}
This compiles and outputs the expected value, but produces a transpiler error: Property 'testMethod' does not exist on type 'TestComponent'. Is there a way to avoid the error? I.E. is it possible to teach Typescript about values mixed into a (React component) class via a decorator?