Skip to content
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

False positives on jsx-no-useless-fragment, need interpolated values to avoid TS errors #2479

Closed
itsMapleLeaf opened this issue Oct 30, 2019 · 3 comments

Comments

@itsMapleLeaf
Copy link

I found another similar closed issue, but it reports different sorts of false positives than those I'm encountering.

In our codebase, we have a pattern of doing return <>{props.children}</>, since returning props.children from function components causes TS errors when children is defined as React.ReactNode. We're trying to avoid using as any or similar. Example:

function LoaderView<T extends ItemStoreObject>(props: LoaderViewProps<T>) {
  useLazyLoad(props.loader);

  const {
    renderError = renderNothing,
    renderLoading = renderNothing,
    loader,
  } = props;

  // The rule warns against all of these returns,
  // but we need them, otherwise we'll get a type error:
  // "Type '{} | null | undefined' is not assignable to type '...'."
  if (loader.data) return <>{props.render(loader.data)}</>;
  if (loader.isLoading) return <>{renderLoading()}</>;
  if (loader.error) return <>{renderError(loader.error)}</>;

  return null;
}

It would be great if the rule could either not report on single interpolated values, or if there were a flag for this case

@ljharb
Copy link
Member

ljharb commented Oct 30, 2019

This sounds like a bug in TS's react types, no?

@itsMapleLeaf
Copy link
Author

It probably is. I guess by the time this issue would have been resolved, a fix might be in TS already, rendering the change useless. The issues to fix the TS bug just seem a little stalled on progress, is all. (DefinitelyTyped/DefinitelyTyped#20356, microsoft/TypeScript#29818)

I'll close. Wrong place to fix this.

@ljharb
Copy link
Member

ljharb commented Oct 30, 2019

In the meantime, an eslint override comment, or a TS ignore comment, might be appropriate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants