Skip to content

Instantly share code, notes, and snippets.

@jrwebdev
Last active June 3, 2020 16:51
Show Gist options
  • Save jrwebdev/683e8c3ac46209a39e06296b4b7f1842 to your computer and use it in GitHub Desktop.
Save jrwebdev/683e8c3ac46209a39e06296b4b7f1842 to your computer and use it in GitHub Desktop.
Loading HOC
interface WithLoadingProps {
loading: boolean;
}
const withLoading = <P extends object>(Component: React.ComponentType<P>) =>
class WithLoading extends React.Component<P & WithLoadingProps> {
render() {
const { loading, ...props } = this.props;
return loading ? <LoadingSpinner /> : <Component {...props as P} />;
}
};
@jrwebdev
Copy link
Author

This appears to be due to a bug with TypeScript:
microsoft/TypeScript#28938 (comment)

To fix it for now you'll need to use a cast:

<Component {...props as P} />;

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