-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lint against {condition && <Element/>}
in JSX
#330
Comments
I'm looking for a linter rule that can help with this.
|
@forabi So TS Lint rules don't apply to TSX? If they do, |
@wholesomedev I think |
I tried the different options for {date && (
<time dateTime={date.toISOString()}>
{' '}
and was last updated on {formatDate(date, 'MMMM D, YYYY')}
</time>
)}
But I don't think we should use it, because it's only accidentally preventing this pattern 😄 The purpose of this rule is to prevent unnecessary fallback values for places where a boolean is expected. Things like It prevents some patterns that I don't see any issue with: const onChange = this.props.onChange || this.defaultOnChange; // I want the return type to always be a `function`, the rule expects a `boolean`.
This is not something I'd want to lint against 🤔. return query || ''; // I want the return type to always be a `string`, the rule expects a `boolean`.
|
export const isHomePage = createSelector(
getRoutingState,
({ location }) => !!location && location.pathname === '/',
);
I don't think this is very useful because the return type is still |
Yeah, I'm not sure what the best way forward is in this case. All we really need is a rule that prevents us from potentially rendering {potentiallyZero && <Element/> /* <= lint error */}
{noWayWillbeZero && <Element/> /* <= ok */} |
For now there isn't an easy way to implement this. Basically, we'll have to write our own TSLint rule, which is not something we're well positioned to do at the moment. |
As we discussed earlier, using
{condition && <Element/>}
in JSX could lead to a bug where0
is outputted to the HTML. I can think of two solutions to this issue:{condition && <Element/>}
and force ternary or another solution in its placeif (condition)
andif (!condition)
. See this twitter thread by Sebastian McKenzie.There might be other solutions, but I can't think of any at the moment.
The text was updated successfully, but these errors were encountered: