import * as React from 'react'; import { ConsumerProps, Context, ProviderProps } from 'create-react-context'; function onlyChild(children: any): any { return Array.isArray(children) ? children[0] : children; } export function combineContext( contexts: { [K in keyof In]: Context } ): Context<{ [K in keyof In]: In[K] }> { class Provider extends React.Component> { render() { const init = this.props.children; return Object.keys(contexts).reduce((acc, contextName) => { const TheContext = contexts[contextName]; return {acc}; }, init); } } class Consumer extends React.Component> { render() { const init = (value: { [K in keyof In]: In[K] }) => onlyChild(this.props.children)(value); const renderer = Object.keys(contexts).reduce<(value: { [K in keyof In]: In[K] }) => any>((acc, contextName) => { const TheContext = contexts[contextName]; return (value: { [K in keyof In]: In[K] }) => ( {contextValue => { return acc({ ...(value as any), [contextName]: contextValue, }); }} ); }, init); return renderer({} as any); } } return { Consumer, Provider, }; }