Looking for a way to inform TypeScript that the output of a function will always meet the type requirements of a variable, therefore avoiding any error messages?
Type 'string | Date' is not assignable to type
?
For example:
const getStringOrDate(asString = true) => {
if (asString) {
return new Date().toLocaleDateString()
} else {
return new Date()
}
}
const today: Date = getStringOrDate(false);
^ Type 'string | Date' is not assignable to type Date