最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using Typescript with Styled Components and Material UI - Stack Overflow

programmeradmin1浏览0评论

Using Typescript with MUI+Styled-Components and you have to directly pass props to MUI elements due to type errors….

const Index = () => {
  return (
      <StyledButton
        variant="contained"
      >
        Hello World
      </StyledButton>
   )}


const StyledButton = styled(Button)`
  background: red;
  color: white;
`;

However, this will error the following:

Type '{ children: string; variant: "contained"; }' is not assignable to type '(IntrinsicAttributes & Pick>) | PropsWithChildren, "form" | "style" | "title" | ... 284 more ... | "variant"> & Partial<...>, "form" | ... 286 more ... | "variant"> & { ...; } & { ...; }) | (IntrinsicAttributes & ... 3 more ... & { ...; })’

When you directly pass in props such as below, then this error goes away. Even using 0 props and 0 children on the Styled MUI element, it gives the error.

const StyledButton = styled(props => <Button {...props} />)`
  background: red;
  color: white;
`;

Using Typescript with MUI+Styled-Components and you have to directly pass props to MUI elements due to type errors….

const Index = () => {
  return (
      <StyledButton
        variant="contained"
      >
        Hello World
      </StyledButton>
   )}


const StyledButton = styled(Button)`
  background: red;
  color: white;
`;

However, this will error the following:

Type '{ children: string; variant: "contained"; }' is not assignable to type '(IntrinsicAttributes & Pick>) | PropsWithChildren, "form" | "style" | "title" | ... 284 more ... | "variant"> & Partial<...>, "form" | ... 286 more ... | "variant"> & { ...; } & { ...; }) | (IntrinsicAttributes & ... 3 more ... & { ...; })’

When you directly pass in props such as below, then this error goes away. Even using 0 props and 0 children on the Styled MUI element, it gives the error.

const StyledButton = styled(props => <Button {...props} />)`
  background: red;
  color: white;
`;
Share asked Apr 10, 2019 at 18:47 user10741122user10741122 8911 gold badge16 silver badges29 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

This should work fine with MUI >= 4.*

For earlier versions, from this tutorial, try enforcing the type of StyledButton:

const StyledButton = styled(Button)`
  background: red;
  color: white;
` as typeof Button;

I accidentally solved this by installing @types/styled-ponents / styled-ponents, which I required anyways to get styled/theme/TS all playing nicely together:

import React from "react";
import styled from "styled-ponents";
import { Theme, useTheme } from "@material-ui/core/styles";
import Button from "@material-ui/core/Button";

const StyledCustomButton: React.FC<{
  theme: Theme;
}> = styled(({ ...props }) => <Button {...props}>Test</Button>)`
  && {
    padding-bottom: ${(props) => props.theme.spacing(2)}px;
  }
`;

const CustomButton: React.FC = () => {
  const theme: Theme = useTheme();
  return <StyledCustomButton theme={theme} />;
};

export default CustomButton;
发布评论

评论列表(0)

  1. 暂无评论