You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Typescript, react-redux and asyncComponent usage? #62
I'm trying to use asyncComponent to load react-redux connected components for code splitting, but I'm having trouble with the asyncComponent Configuration's resolve typescript definitions. I'm not sure if I'm doing something wrong, or if there is an incompatibility between the return type of react-redux's connect and the resolve return type.
Very cut-down component setup example:
'./components/Page.tsx':
Hopefully I haven't made any critical typos in the example.
What I am getting is Typescript is rejecting the AsyncPage's resolve function.
Message is:
Types of property 'resolve' are incompatible.
Type '() => Promise<typeof "./containers/PageContainer">' is not assignable to type '() => Promise<ComponentType<{}>>'.
Type 'Promise<typeof "./containers/PageContainer">' is not assignable to type 'Promise<ComponentType<{}>>'.
Type 'typeof "./containers/PageContainer"' is not assignable to type 'ComponentType<{}>'.
Type 'typeof "./containers/PageContainer"' is not assignable to type 'StatelessComponent<{}>'.
Type 'typeof "./containers/PageContainer"' provides no match for the signature '(props: { children?: ReactNode; }, context?: any): ReactElement<any> | null'.
What do I need to provide by the way of type information to asyncComponent/Configuration to be able to use this with a connected component?
I think this is due to the autoResolveES2015Default param of the asyncComponent definition. It defaults to true, so it's hiding that implementation detail from the TypeScript compiler. I've been manually resolving like this:
It keep the compiler happy so it doesn't think you're trying to do anything silly. The advantage with using ComponentType<T> being that when you import the async component elsewhere for use as a JSX element, the TypeScript compiler can check and give suggestions on props.
Edit: I gave another look over your mods to the type definition and see that with your change to the resolve method you would still gain the ability to get prop checks/suggestions (if you explicitly provide your props Type) when importing the async component, while sacrificing the ability to properly type check the resolve callback itself. This would allow interoperability with the autoResolveES2015Default as you've demonstrated, but albeit with a trade off on a more generic (and I'm not sure semantically correct) definition.
Reacted by Steve Clay, 卫斯理, catstuart and Emre Keskin
Activity
dawnmist commentedon Oct 13, 2017
Got it finally. If I change the definition for resolve to return a React.ReactNode like this:
and then pass the Own Props definition into the asyncComponent call:
it starts working.
bdoss commentedon Oct 13, 2017
I think this is due to the
autoResolveES2015Defaultparam of the asyncComponent definition. It defaults to true, so it's hiding that implementation detail from the TypeScript compiler. I've been manually resolving like this:resolve: () => import('MyComp').then(x => x.default)It keep the compiler happy so it doesn't think you're trying to do anything silly. The advantage with using
ComponentType<T>being that when you import the async component elsewhere for use as a JSX element, the TypeScript compiler can check and give suggestions on props.Edit: I gave another look over your mods to the type definition and see that with your change to the resolve method you would still gain the ability to get prop checks/suggestions (if you explicitly provide your props Type) when importing the async component, while sacrificing the ability to properly type check the resolve callback itself. This would allow interoperability with the
autoResolveES2015Defaultas you've demonstrated, but albeit with a trade off on a more generic (and I'm not sure semantically correct) definition.dawnmist commentedon Oct 14, 2017
Ah, thank you - that works. I'll close my pull request, and put in a different one to update the docs with a typescript usage example instead :)
ctrlplusb commentedon Oct 16, 2017
@dawnmist - legend thank you for that!