Feature Request: `Boolean()` behaviour vs `!!` operator
hubastard opened this issue · comments
Hubert Gendron commented
Boolean()
constructor does not behave like the !!
operator. Boolean is useful in react conditional rendering when checking for an optional string to avoid rendering issues.
When we use !!
typescript understands that the value cannot be optional/falsy anymore.
For example:
<>
{!!label && <Text>{label.toUpperCase()}</Text>}
</>
But when you use Boolean()
typescript still thinks that the value can be optional/falsy. Which forces you to unwrap the value using !
or add ?
.
For example:
<>
{Boolean(label) && <Text>{label!.toUpperCase()}</Text>}
{Boolean(label) && <Text>{label?.toUpperCase()}</Text>}
</>
Hubert Gendron commented
This declaration seems to work well for me.
interface BooleanConstructor {
new (value?: any): Boolean;
<T>(value?: T): value is TSReset.NonFalsy<T>;
readonly prototype: Boolean;
}