I was working with Typescript and got confused with how the compiler infers the return types differently for these two functions.
//this is of type () => boolean | ""
const isFormValid = () => {
return jobTitle && jobTitle!=''
}
//this is of type () => boolean
const isFormInvalid = () => {
return !jobTitle || jobTitle===''
}
Yes, jobTitle
is a string, but shouldn't both of these be of return type () => boolean
?
isFormValid('') === '' // true
If you want boolean as returned value use double !
:
const isFormValid = () => {
return !!jobTitle && jobTitle!=''
}