Need help with inferring a higher-order component
Hi! I'm trying to create a higher-order component in React that enhances a given component with an additional type. This additional type is used internally of the HOC as it's the return type of the data that's being fetched. The wrapped component will receive the props from the extra data and the rest of the props that the component accepts.
To keep the example simple, I've simplified the HOC a bit and it looks like this:
I would like to use the HOC like this:
However, this doesn't work as the second type helper for
withApplicationData
doesn't get inferred correctly and it's currently mandatory, meaning I have to use the following code to make it work:
This is redundant as the props can be inferred from the passed component to the HOC.
Is there a way to infer this type correctly? So that I don't have to pass a second type argument each time I want to use this HOC?Solution:Jump to solution
Asked this question in a different Discord group and found out that this can only be done by adding an extra curried function. When the following GitHub issue gets resolved, which has been open since 2018, the second type helper can probably be inferred:
https://github.com/microsoft/TypeScript/issues/26242...
GitHub
Proposal: Partial Type Argument Inference · Issue #26242 · microsof...
After exploring the concept in #23696, we've come to the conclusion that implicitly making type arguments available by name would unnecessarily expose previously unobservable implementation det...
2 Replies
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
Solution
Asked this question in a different Discord group and found out that this can only be done by adding an extra curried function. When the following GitHub issue gets resolved, which has been open since 2018, the second type helper can probably be inferred:
https://github.com/microsoft/TypeScript/issues/26242
GitHub
Proposal: Partial Type Argument Inference · Issue #26242 · microsof...
After exploring the concept in #23696, we've come to the conclusion that implicitly making type arguments available by name would unnecessarily expose previously unobservable implementation det...