Skip to content

Instantly share code, notes, and snippets.

@gsasouza
Created July 20, 2020 15:26
Show Gist options
  • Save gsasouza/3c6aee09819d74197aca49618411bb3d to your computer and use it in GitHub Desktop.
Save gsasouza/3c6aee09819d74197aca49618411bb3d to your computer and use it in GitHub Desktop.
const StyledGenericComponent = styled.div``;
type GenericProps<T> = {
as?: React.FC<T>
//...otherProps
}
const GenericComponent = <T>({ as, ...props }: Props<T> & T) => {
// some logic
return <StyledGenericComponent as={as} {...props}/>
}
const UseExample = () => (
<>
<GenericComponent<{ prop1: number }>
prop1={1}
prop2={3} // type checker shows an error
/>
<GenericComponent<{}>
prop1={1} // type checker shows an error (prop1 doens't exist e Prop)
/>
<GenericComponent
prop1={1} // type check doesn't shows an error (prop1 doens't exist e Prop)
/>
</>
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment