In my project, I have organized three classes: Chat
, Quest
, and Receiver
.
The main challenge was to allow another class, specifically Class Quest
, to access three specific actions (methods) without tightly coupling the classes. To achieve this, I created a getter function called chatInterface
in the Chat
class to expose the methods and pass them to the Receiver
class. Additionally, I utilized the bind
method to retain the correct context of this
.
Eventually, two of the actions (actionB
and actionC
) needed to be moved to a third class named Quest
. These actions are now exposed through a different getter within the Quest
class.
Now, the question arises whether there is a way to combine these three methods—one from the Chat
class and two from the Quest
class—and pass them as a single type to the Receiver
class while preserving the correct context.
For reference, here's a simplified code snippet:
type ChatInterface = {
actionA(): void;
}
type QuestInterface = {
actionB(): void;
actionC(): void;
}
class Chat {
readonly quest: Quest = new Quest();
readonly receiver: Receiver = new Receiver(this.chatInterface, this.quest.questInterface);
get chatInterface(): ChatInterface{
return {
actionA: this.actionA.bind(this),
}
}
actionA() {
}
}
class Quest{
get questInterface(): QuestInterface{
return {
actionB: this.actionB.bind(this),
actionC: this.actionC.bind(this),
}
}
actionB() {
}
actionC() {
}
}
class Receiver {
constructor(private readonly chatInterface: ChatInterface, private readonly questInterface: QuestInterface){}
}