【发布时间】:2021-09-19 06:19:42
【问题描述】:
我一直在试图弄清楚如何解决这个过载问题,但到目前为止还没有骰子。我正在使用 Typescript、Styled-components 和 material-ui。我正在使用 styled(MUIButton) 构建原生 MUI Button。通过 console.logs 我可以看到我的 props 传递到组件中,但是我不知道如何设置类型,所以重载错误消失了。
有什么想法吗?
相关文章:How to extend props for Material-UI components using Typescript?
错误:
代码:
import { default as MUIButton, ButtonProps } from '@material-ui/core/Button';
import styled, { css } from 'styled-components';
import { darken } from '@material-ui/core/styles';
interface IButton extends ButtonProps {
format?: 'primary' | 'secondary';
variant?: 'contained' | 'outlined' | 'text';
label: string;
}
const Button = ({ format, variant, label }: IButton): JSX.Element => (
<StyledButton format={format} variant={variant}>
{label}
</StyledButton>
);
Button.defaultProps = {
format: 'primary',
variant: 'contained',
};
export default Button;
const StyledButton = styled(MUIButton)<IButton>`
//? contained variant (default)
${({ format, theme }) => `
background-color: ${
format === 'primary'
? theme.palette.colors.main
: theme.palette.colors.secondary
};
color: ${theme.palette.font.secondary};
font-weight: ${theme.agnosticStyles.font.weight.bold};
padding: .7rem 4rem;
&:hover {
background-color: ${darken(theme.palette.colors.main, 0.2)};
}
`}
...
}
【问题讨论】:
-
请提供包的版本
-
它是一个新项目,所以它的所有最新版本
-
它在 ts playground 中抛出错误
标签: reactjs typescript material-ui styled-components