Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save janjakubnanista/3346b1bd31cac81680fea0db4fdafb3a to your computer and use it in GitHub Desktop.
Save janjakubnanista/3346b1bd31cac81680fea0db4fdafb3a to your computer and use it in GitHub Desktop.
// Approach 2: diving back in looking for a better way
//
// We can create a type that accepts a type parameter
// and puts a constraint on it, in our case we will demand
// the type parameter to be a React.FC
type AssertComponent<C extends React.FC<any>> = C;
// Then we use it in our Select component
function Select<T>(props: SelectProps<T>) {
return null;
}
// If the Select does not look like a React component, this line will trigger an error
type AssertSelect = AssertComponent<typeof Select>;
// However we ended up with an unused type AssertSelect :(
// And we can still pass invalid defaultProps :(((
Select.defaultProps = 7;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment