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} />;
}
};
@mjmaix
Copy link

mjmaix commented Feb 9, 2019

I'm having TSLint error [ts] Type '{}' is not assignable to type 'P'. [2322] do you know how to fix this?
image

@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