Proposal: set condition type to any
faiwer opened this issue · comments
Hello. I have one tiny proposal. What about changing the typing for pseudo If-component's condition
property from boolean
to any
.
Motivation:
<If condition={!!(a && a.someField)}>...</If>
// =>
<If condition={a && a.someField}></If>
the same reason why we use if (some)
instead of if (Boolean(some))
or if (!!(some))
sounds good
I don't believe this is a good idea in general, as losing type safety to any
will render some useful linting rules useless.
IMO it would be better to keep it as boolean
and use the !!
approach in case someone does not want to follow the path of strict boolean expressions.
I think there are few people who prefer to write !!()
. Because the whole language doesn't work this way. E.g.:
if
while
repeat until
array.filter
array.find
array.some
None of them ^ demands to have strict boolean values.
And TS itself always chooses practicality to strictness.
To be honest I don't even know a language where developers has to write:
if (!!variable) {
}
Moreover actually it works unstrictly.
BTW we always can override default types. I did it in our projects.
I am not entirely persuaded by this argument. I understand how reducing type safety can result in a more concise code, but I personally don't see concise code as the ultimate goal, especially when you lose something along the way.
Indeed one can do things very concisely in pure JS, at the expense of occasionally shooting oneself in the foot, and the whole thing not being scalable as a result. Typing stuff as any
is not really different from writing pure JS.
If we want to check that a variable is defined, and the boolean
condition type (seemingly) makes it impossible, we can always write a helper function called isDefined(value: unknown)
and pass that into the condition. IMO it would much better communicate your intent while not circumventing the type safety.
Finally, as @faiwer mentioned, since the default library typings can always be overridden, I would rather prefer them to be strict by default, and optionally relaxed for projects where type safety matters less, but not vice versa.
It may sound crazy :-) but what's about this:
<IfStrict condition={mustBeBoolean}>...</IfStrict>
<If condition={hasToBeTruthy}>...</If>
The value of condition
should behave in the same way, imo, as any old if
or conditional - testing truthiness. Forcing boolean values unnecessarily creates this strict check which is overkill for most use cases, at least in my experience. Forcing consumers to use !!
is ugly.
done d7c8ac2