Since Javascript often requires me to check if a value is `!= null && != ''`, I decided to create a function that checks for empty values:
const isEmpty = (variable: any, allowEmptyString?: boolean): boolean => {
return variable == null || (variable == '' && !allowEmptyString);
};
The downside is that other methods are not aware of what this function does, so I constantly need to use `!` to prevent warnings. For example:
const foo = (val?: number): void => {
let a = 0;
if (!isEmpty(val)) {
a = val;
// let a: number;
// Type 'number | undefined' is not assignable to type 'number'.
// Type 'undefined' is not assignable to type 'number'
}
};
Currently, my workaround is:
if (!isEmpty(val)) {
a = val!
}
Is there a way to avoid using `!` to prevent the warnings?