export async function getSingleUserQA(command: Command, period?: string[]) {
let slackUserID;
if (command && command.text) {
slackUserID = command.text.match("/(?<=@)(.*?)(?=|)/g")[0];
}
}
I am being flagged that 'command' is possibly 'null' or 'undefined'. How come if I am making sure that command exists and command.text exists?
CodePudding user response:
Look closely at the warning. It's not saying that command
might not exist, but that this whole expression:
command.text.match("/(?<=@)(.*?)(?=|)/g")
might not.
What do you want to do if the regular expression has no match? Figure that out, and guard it appropriately.
if (command && command.text) {
const match = command.text.match("/(?<=@)(.*?)(?=|)/g");
if (match) {
slackUserID = match[0];
} else {
// handle badly formatted command
}
}
You should also probably do
let slackUserID: string;
or only use the result inside the conditional.