See it in action at https://codesandbox.io/s/n6txl.
Have you ever come across a tree like this:
<ContextA.Provider>
<ContextB.Provider>
<ContextC.Provider>
...
</ContextC.Provider>
</ContextB.Provider>
</ContextA.Provider>
Instead, we can compose/combine this list of providers:
<Compose items={[
[ContextA, props],
[ContextB, props],
[ContextC, props],
]}>
...
</Compose>
The advantage of above code is to allow Providers to have different type, and each items would check against itself for props, while using only one generic T, will limit to only one kind of Provider
https://codesandbox.io/p/sandbox/sharp-bohr-cwh4ff?file=%2Fsrc%2FApp.tsx%3A27%2C19
It actually support more than 6 nested components with last bit T type as fallback, but it will lose the ability to check type for each items. I use 6 here just good enough for most case, so feel free to add more.
ComponentsProps
my bad typos, changed toComponentProps
,p.s. still waiting a better Utility Type for compose in typescript (ref: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/lodash/fp.d.ts#L304)