-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TypeScript] Error when using the component-property on Buttons with react-router-dom #13218
Comments
That's a current limitation of our type declarations. We would like to make them generic so that the type checker would know what additional props it can accept but we would loose argument type inference for callbacks like A working solution would be the second example of the linked section (under "avoid properties collisions"). A word of advice: Try to enable |
@Schleuse I temporarily have an ugly component (based on @eps1lon's suggestion that acts as a terrible workaround until all of this gets sorted out... anyone please feel free to improve this concept in the meantime import * as React from "react";
import Button, { ButtonProps } from "@material-ui/core/Button";
import { Link } from "react-router-dom";
import { History } from "history";
/**
* Issue from: https://github.com/mui-org/material-ui/issues/13218
*/
interface NavbarLinkButtonProps extends ButtonProps {
to: History.LocationDescriptor;
children: any;
}
const NavbarLinkButton: React.SFC<NavbarLinkButtonProps> = ({
to,
children,
...rest
}) => {
const NavbarLink: React.SFC<any> = props => <Link to={to} {...props} />;
return (
<Button component={NavbarLink} {...rest}>
{children}
</Button>
);
};
export default NavbarLinkButton;
// ... use case
// <NavbarLinkButton to="/home">Home</NavbarLinkButton> |
@mcabs3 Could you ellaborate on why this is "ugly" or "terrible"? The only issue I can see is the I will look into this again. We could additionally export generic props and see if those are more widely adopted. |
@eps1lon assigning a props type as The main reason I don't like it is the fact I have to have a component definition within another component.
Maybe I'm just being picky. |
Component prop example was mainly concerned with react-router + button so we just added a proper demo for it. Closes #13218
Component prop example was mainly concerned with react-router + button so we just added a proper demo for it. Closes mui#13218
Expected Behavior
I'm trying to use React-Router-dom with Buttons. Thankfully material-ui provides a example for this exact case in the docs.
The described approach in the docs does not seem to work properly when using Typescript. It seems that the Props of the passed Component does not widen the Props of the Button. Therefore I get a error that the passed
to
-Attribute does not exist in the Props of Button.Steps to Reproduce
Link: https://codesandbox.io/s/8n40vqy3k0
src/pages/index.tsx
to see the errorYour Environment
tsconfig.json
The text was updated successfully, but these errors were encountered: