DEV Community

loading...

Discussion on: It's time to let go of lodash

michaelcurrin profile image
Michael

Okay thanks. I didn't understand your TS before.

Yes if you have user input you could pass non accepted type to a function. That is a risk for all types and not just boolean and there are a few ways to do that.

Like myVar as Boolean.
Or if your input can't be trusted, then add some sanitization on your form handling or use a check before or within the function as typeof myvar === 'boolean.

Thread Thread
jmitchell38488 profile image
Justin Mitchell Author • Edited

Like I said earlier, !!prop and Boolean(prop) are fundamentally the same - they determine the truthiness of a value by coercion, not if it is in fact a boolean, just that it has a value other than "", null, undefined, false and 0. Given an array and object share the same super type object, truthiness check will always return true.

typeof myvar === boolean isn't required when you can just do prop === true || prop === false and explicitly check the values, rather than the type.

Lodash explicitly checks the truthiness of the var, and some other checking for arrays and objects. My suggestion isn't a hard and fast rule, it's just one of many options and it's a useful shorthand check. Feel free to use !!var, Boolean(var) or copy+paste the lodash code.

Thread Thread
michaelcurrin profile image
Michael

Ok thanks. My original point was not on the best way to do truthiness though but the fact that these two are not interchangeable as you had suggested, so was recommending it be fixed.

_.isBoolean(true); //true
Boolean(true); //true - same as above line
Enter fullscreen mode Exit fullscreen mode
_.isBoolean(false); //true
Boolean(false); //false. But different result to above line and therefore not equivalent
Enter fullscreen mode Exit fullscreen mode