I'm trying to define re-usable React components that accept a React.Context field through a prop. They may not need the entirety of the properties available in the parent's context and, given the desire for reuse, may be encapsulated in Providers with different Context structures (but the same core properties needed by the reused sub component). For instance a parent provider higher in the tree may define the Context type like so:
type SuperSet = {
x: number,
y: number,
z: number
}
let superSet = {x: 1, y: 2, z: 3}
const SuperSetContext = React.createContext<SuperSet>(superSet)
const SuperSetProvider = (props) => {
return (
<SuperSetContext.Provider value={superSet}>
...
{/* Arbitrarily deep nested component in the tree, most likely in a different file*/}
<SubComponent Context={SuperSetContext} />
</SuperSetContext.Provider>
);
}
The SubComponent should (I believe) be able to define a Context prop with less properties like so
const SubComponent: React.FunctionComponent<{
Context: React.Context<{x: number, y: number}>
}> = ({ Context }) => {
const { x, y } = useContext(props./Context);
return (<div>{x + y}</div>)
}
Or via Pick<>
Context: React.Context<Pick<SuperSet, 'x' | 'y'>>
However either way the above SubComponent causes a type error when the prop is assigned within the Provider
<SubComponent Context={SuperSetContext} />
Type 'Context<SuperSet>' is not assignable to type 'Context<SubSet>'.
Types of property 'Provider' are incompatible.
Type 'Provider<SuperSet>' is not assignable to type 'Provider<SubSet>'.
Types of parameters 'props' and 'props' are incompatible.
Type 'ProviderProps<SubSet>' is not assignable to type 'ProviderProps<SuperSet>'.ts(2322)
test.tsx(26, 3): The expected type comes from property 'Context' which is declared here on type 'IntrinsicAttributes & { Context: Context<SubSet>; } & { children?: ReactNode; }'
I created a Typescript Playground to test it without jsx but it occurs regardless of using jsx. Additionaly I don't see the same behavior with naive generic classes/functions.
So is there a way to define the SubComponent's Context definition with a subset or Context properties OR a different paradigm to accomplish the same design and escape this particular typing mismatch?