Skip to content

Instantly share code, notes, and snippets.

@mattiamanzati
Created May 16, 2020 10:19
Show Gist options
  • Save mattiamanzati/d948ef6ba8186d7b21d517b5ef3cda6d to your computer and use it in GitHub Desktop.
Save mattiamanzati/d948ef6ba8186d7b21d517b5ef3cda6d to your computer and use it in GitHub Desktop.
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