The thing I'd like to achieve is to create generic-typed component which would accept different props (based on its usage in project), then finally output its' children prop with all the given props (flow-through) + possibly some new consts (which would be calculated based on the props), like so:
const bonus1 = { offerId: 1, offerDate: 1911 }
const bonus2 = { offerId: 2, timeLeft: 100 }
const bonus3 = { offerId: 3, offerDate: 1991, isActive: true }
<Bonus {...bonus1}>
{({ offerId, offerDate, onClick }) => {})
</Bonus>
<Bonus {...bonus2}>
{({ offerId, timeLeft, onClick }) => {})
</Bonus>
<Bonus {...bonus3}>
{({ offerId, offerDate, isActive, onClick }) => {})
</Bonus>
I've prepared some common type with fields I know every bonus
would have, like: offerId
:
type BonusCommon = { offerId: number }
Now the component types:
type BonusProps<T> = {
children: (renderProps: RenderProps<T>) => ReactNode
} & T
type RenderProps<T> = {
onClick: () => void
} & T // here is what I thought would work, ie. pass to `children` all the input props
And the component itself:
const Bonus = <T extends BonusCommon>({
children,
offerId,
...rest, // the rest of the input props
}: BonusProps<T>) => {
const onClick = () => {} // do something with available props
return (
<>
children({ // TS ERROR!
onClick,
offerId,
...rest
})
</>
)
}
I am out of ideas that would actually make it work, cos currently there's an error saying: 'T' could be instantiated with a different subtype of constraint 'BonusCommon'