import React, { lazy, Suspense } from 'react'; interface Opts { fallback: React.ReactNode; } type Unpromisify = T extends Promise ? P : never; export const lazyLoad = < T extends Promise, U extends React.ComponentType, >( importFunc: () => T, selectorFunc?: (s: Unpromisify) => U, opts: Opts = { fallback: null }, ) => { let lazyFactory: () => Promise<{ default: U }> = importFunc; if (selectorFunc) { lazyFactory = () => importFunc().then(module => ({ default: selectorFunc(module) })); } const LazyComponent = lazy(lazyFactory); return (props: React.ComponentProps): JSX.Element => ( ); };