Created
May 16, 2020 10:19
-
-
Save mattiamanzati/d948ef6ba8186d7b21d517b5ef3cda6d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
interface ButtonProps { | |
shape: 'circle'|'rectangle'|'oval' | |
} | |
function Button(props: ButtonProps) { | |
// renders some pretty button | |
return <button>Heyo</button> | |
} | |
class Button2 extends React.Component<ButtonProps> { | |
render(){ | |
return <button>LOL</button> | |
} | |
} | |
// Only Button is exported from a library; its props are not. | |
// Now I want to wrap it in my own component, and inherit all of Button's props | |
type GetComponentProps<A> = A extends React.FunctionComponent<infer P> ? P : (A extends React.ComponentClass<infer P> ? P : never) | |
type MyButtonProps = GetComponentProps<typeof Button2> & { | |
// ^^^^^^^^^^^^^^^^^^^^^-------- SOMETHING LIKE THIS??? | |
anotherFeature: string | |
} | |
function MyButton({anotherFeature, ...rest}: MyButtonProps) { | |
return <Button {...rest}>Sup</Button> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment