MUI v5中FormControl使用fullWidth报TS2322属性不存在如何解决?
错误原因
核心问题是你手动为styled生成的FormControl组件指定了错误的props类型,仅声明了theme属性,完全覆盖了MUI原生FormControl自带的属性类型定义,导致TypeScript无法识别fullWidth、hiddenLabel等合法属性。
修复方案
- 第一步:删除手动添加的错误类型注解,MUI的styled工具会自动继承原组件的所有属性类型,无需额外声明:
// 原来的错误写法 // const FormControl: React.FC<{ theme: Theme }> = styled(MuiFormControl)(({ theme }) => ({ // 修复后的写法 const FormControl = styled(MuiFormControl)(({ theme }) => ({ color: theme.palette.nav.links, borderRadius: 30, width: 180, }))
- 可选优化:如果需要让
fullWidth属性的样式生效,需要调整宽度逻辑避免固定宽度覆盖100%宽度设置:
const FormControl = styled(MuiFormControl)(({ theme, fullWidth }) => ({ color: theme.palette.nav.links, borderRadius: 30, width: fullWidth ? '100%' : 180, }))
内容的提问来源于stack exchange,提问作者Jamie Hutber
相关产品推荐
相关产品推荐

