我想设置一个使用 Material-UI v4.11.4 的 Typescript 项目来使用情感进行样式设置,为 MUI v5 版本做准备。目标是在项目中引入情感,以便开发人员可以开始使用新样式,而不是通过即将弃用的 makeStyles
继续编写样式。功能。
除了我在 css
上从 Typescript 收到类型错误之外,我一切正常支柱。我一直在搜索文档以寻找答案,但似乎找不到任何东西。为了让 Typescript 理解 css
,我需要安装一些额外的类型包吗? MUI 组件上存在 prop 吗?
import { Button } from "@material-ui/core";
const Btn = () => {
return (
// this successfully applies styles, but causes a TS error
<Button css={{ backgroundColor: "red" }}>
Click Me
</Button>
);
}
TS错误:属性 'css' 不存在于类型 'IntrinsicAttributes & { href: string; } & { child ?:ReactNode;颜色?:颜色 |不明确的;禁用?: bool 值 |不明确的; disableElevation?: boolean |不明确的; ……还有6个……;变体?:“文本” | ... 2 更多... |不明确的; } & { ...; } & CommonProps<...> & Pick<...>'。
最佳答案
嗯,我觉得很傻。我以为这是 MUI 没有正确导出类型的问题,但结果我只需要设置 jsxImportSource
在我的 tsconfig.json 中
"compilerOptions": {
"jsxImportSource": "@emotion/react"
}
关于reactjs - 无法让 Typescript 理解 Material-UI 组件上的 css Prop (情感),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67451283/