I have a function below, that checks if myValue exists before calling doSomething(), if it does not exist, it will throw. Typescript accepts this...
function myFunction(){
if (!myValue) {
const error = Error(errorName);
error.name = errorName.replace(/\s+/g, "-").toLowerCase();
throw error;
}
const something = doSomething(myValue);
}
The below implementation makes a Helper Function for throwing a named Error. Typescript doesn't like this, and errors that doSomething(myValue) is being passed myValue which is potentially undefined. How can I type the namedError so that it has the inferred typing from above, that it is Truthy?
function myFunction(){
if (!myValue) {
namedError("my value doesn't exist");
}
const something = doSomething(myValue);
}
function namedError(errorName: string){
const error = Error(errorName);
error.name = errorName.replace(/\s+/g, "-").toLowerCase();
throw error;
};