【发布时间】:2019-09-01 07:37:45
【问题描述】:
将 Typescript 与 MUI+Styled-Components 一起使用,由于类型错误,您必须直接将 props 传递给 MUI 元素......
const Index = () => {
return (
<StyledButton
variant="contained"
>
Hello World
</StyledButton>
)}
const StyledButton = styled(Button)`
background: red;
color: white;
`;
但是,这会报错:
键入'{孩子:字符串;变体:“包含”; }' 不可分配给类型 '(IntrinsicAttributes & Pick>) | PropsWithChildren,“形式” | “风格” | “标题” | ... 284 更多... | "变体"> & 部分<...>, "形式" | ... 286 更多... | “变体”> & { ...; } & { ...; }) | (IntrinsicAttributes & ... 还有 3 个 ... & { ...; })'
当你像下面这样直接传入 props 时,这个错误就会消失。即使在 Styled MUI 元素上使用 0 个道具和 0 个子元素,也会出现错误。
const StyledButton = styled(props => <Button {...props} />)`
background: red;
color: white;
`;
【问题讨论】:
标签: javascript reactjs typescript material-ui styled-components