My goal is to design a Question
class that requires various types of responses for different scenarios. These responses can be in the form of a DoAnswer
(e.g. "run") or a CheckAnswer
(e.g. "check the clock"). I have created separate enums for these response types since there are only a limited number of possible actions and checks in my specific case, each needing a unique name and display string, making enums the ideal choice. Now, I am looking for a way to store the expected answer type for each question.
I had considered using something like answerType: enum;
, but this approach does not work as an enum cannot be used as a value type. In Java, you could use Class<?> answerType
, but I am unsure how to achieve similar functionality in TypeScript.
The reason behind needing this information is that I want to determine the correct answer based on the expected type for a given question, like so:
this.correctAnswers: DoAnswer[] | CheckAnswer[];
if(answerType is DoAnswer) {
this.correctAnswers = answerService.calculateCorrectDoAnswers();
// Here, answerService retrieves possibleAnswers and situationInQuestion
}
It's possible that I may be misunderstanding some key concepts, but at the moment, I am unable to see a more effective solution.