Presented below is the snippet of code:
type Bot = BotActive | BotInactive;
class BotActive {
public readonly status = "active";
public interact() {
console.log("Hello there!");
}
}
class BotInactive {
public readonly status = "inactive";
}
function canInteract(bot: Bot) {
if (bot.status === "inactive") throw Error("Bot cannot interact when it's inactive!");
bot.interact();
}
Now, I am interested in verifying the status of the bot using a TypeScript assertion function! Here's what I have in mind:
function canInteract(bot: Bot) {
assertBotStatus(bot, "active");
bot.interact();
}
Is there a way to achieve this? If so, how would the assertBotStatus
function be implemented?
Your help is greatly appreciated!