Currently, I am still in the process of learning Typescript and Javascript so please bear with me if I overlook something.
The issue at hand is as follows:
When calling this.defined(email), VSCode does not recognize that an error may occur if 'email' is undefined. This discrepancy arises from validateEmail() accepting a string? argument, leading the compiler to believe it could potentially receive an undefined value (as per my understanding). Is there a way to reassure the compiler that this scenario is acceptable? Or am I overlooking a crucial detail?
I intend to utilize Validate.validateEmail() in other classes as well, which poses the same problem in those scenarios too.
Validate.ts
export default class Validate {
static validateEmail(email?: string) {
// TODO: Properly test this regex.
const emailRegex = new RegExp('^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$')
this.defined(email);
this.notEmpty(email);
if (!emailRegex.test(email)) {
throw new SyntaxError("The email entered is not valid");
}
}
static defined(text?: string) {
if (!text) {
throw new SyntaxError("The text recieved was not defined.");
}
}
static notEmpty(text: string) {
if (text.length < 1) {
throw new SyntaxError("The text entered is empty.");
}
}
}