I've been reviewing a case that looks like this:
loggedInUser$ = this.select().pipe(
filter(({ user }) => toBoolean(user)),
map(({ user: { firstName: f, lastName: l } }) => `${f} ${l}`)
);
Just curious whether we could always substitute !!
in place of this method to get a boolean and whether. IIUC the semantics would always be the same?
In other words we should always be able to replace toBoolean(...)
with !!
?
The implementation looks like this:
// @internal
export function toBoolean(value: any): boolean
{
return value != null && `${value}` !== 'false';
}
Analysis
So based on the answers the difference is that !!
returns true for 'false', but toBoolean()
returns false
for 'false'.
This is a bit subjective, but personally I feel that it's better to tell users to use !!
than some other sugared approach, since we should be familiar with the Javascript basics / semantics first and then build out from that.
Thus if anyone wants 'false'
to be false, then they have to implement that explicitly. The application in this case would be that someones name is actually 'false` and we want to allow that to be true.