You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using the &&, ?, || or other symbols for narrowing types whilst controlling flow is terse, but often less readable. This is even more true for people new to javascript. Using the Boolean function makes the syntax makes this more explicit. From personal anecdotal experience, it's been a preferred style for a number of teams.
The text was updated successfully, but these errors were encountered:
Using ... symbols for narrowing types whilst controlling flow is terse, but often less readable. This is even more true for people new to javascript.
People like to say this, but IMO if (!!obj.foo) strikes me as much clearer in intent to someone unfamiliar with the language than if (Boolean(obj.foo)). For the former I can infer that !! implies some kind of existence check, while the latter requires me to understand upfront the rules for boolean coercion in JS. It's not immediately obvious that it's checking for non-nullish values.
Suggestion
π Search Terms
boolean narrow optional control "if(Boolean("
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
When using the
Boolean
function to check if an optional object property is present, this should narrow the type of the checked property.π Motivating Example
π» Use Cases
The pattern of controlling "if this is present, do X, else do Y" is one of the basics of dealing with optional properties.
Consider the ubiquity of the following code in React applications:
Using the
&&
,?
,||
or other symbols for narrowing types whilst controlling flow is terse, but often less readable. This is even more true for people new to javascript. Using theBoolean
function makes the syntax makes this more explicit. From personal anecdotal experience, it's been a preferred style for a number of teams.The text was updated successfully, but these errors were encountered: