web: usage of the conditional Wildcard component generic
Created by: valerybugakov
Context
I can't use conditional as in Card component (using as={Button} or as="div" works fine). This is the error I get:
The expected type comes from property 'as' which is declared here on type 'IntrinsicAttributes & Omit<Pick<DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "key" | keyof HTMLAttributes<HTMLDivElement>> & { ref?: ((instance: HTMLDivElement | null) => void) | RefObject<HTMLDivElement> | null | undefined; }, "as"> & CardProps & { as?: "div" | undefined; }'
Changes
- The
ForwardReferenceComponent
helper is changed to allow passing generic props that would be used instead of the component own prop types. It's a copy-paste from this PR: https://github.com/sourcegraph/sourcegraph/pull/30096. - The conditional
as
value is extracted into a variable for reusability:
const divOrButton = completed ? 'div' : Button
type DivOrButton = typeof divOrButton
- Then it's used to pass the right generic type to the
Card
component:
<Card<DivOrButton, React.ComponentProps<DivOrButton>>
as={divOrButton}
>
<div />
</Card>