Currently, I am conducting tests using Typescript to explore the feasibility of a project I have in mind. In one of my functions, I need to specify the type of 'this' as a class. While it technically works, I continue to encounter an error message stating: "The 'this' context of type 'void' is not assignable to method's 'this' of type 'Rule'." Below you will find the relevant code snippet.
Summary: How can I define the type of 'this' within a standalone function?
Note: Perhaps there exists a more efficient approach to achieve this, so let me elucidate my objective: I aim to create a rule class with a function that simply returns true or false along with additional notes and a status. I prefer not to overload the function with multiple parameters. The class should be able to interact with other classes, access external data, while also being standalone for ease of testing.
export class Rule {
constructor(rule) {
this.Fx = rule
}
Fx;
passNote;
failNote;
guest;
apiData;
runRule(dev) {
this.Fx(dev)
}
}
function apple(this: Rule, dev) {}
const test = new Rule((dev) => dev.pass)
const dev = // large object I am testing with the rule
test.runRule(dev)
// The 'this' context of type 'void' is not assignable to method's 'this' of type 'Rule'
My main goal is to ensure proper autocomplete functionality for 'this' when crafting the function utilized in the constructor.