长话短说,typescript抱怨property classes
每个 material-ui 组件缺失。换句话说,Typescript 强制该classes
属性存在于几乎每个 material-ui 组件中。这是使用 webpack 编译期间的错误消息ts-loader
,
我有以下代码,
头文件.tsx
import AppBar from 'material-ui/AppBar';
import * as React from 'react';
import { withStyles } from 'material-ui/styles/index';
import Toolbar from 'material-ui/Toolbar';
import Typography from 'material-ui/Typography';
const decorate = withStyles((theme) => ({
header: {
paddingTop: theme.spacing.unit,
width: '100%',
},
}));
const AppBarTypographyStyle = {
color: 'white',
fontSize: '1.6rem',
};
const Header = decorate(({classes}) => (
<header>
<AppBar position="static" classes={{root: classes.header}}>
<Toolbar>
<Typography style={AppBarTypographyStyle} classes={{}} color="inherit" type="title">
OLIVE
</Typography>
</Toolbar>
</AppBar>
</header>
));
export default Header;
我有Toolbar
以下消息的错误
TS2322: Type '{ children: Element; }' is not assignable to type 'IntrinsicAttributes & ToolbarProps & { children?: ReactNode; }'.
Type '{ children: Element; }' is not assignable to type 'ToolbarProps'.
Property 'classes' is missing in type '{ children: Element; }'
对于AppBar
和Typography
我加入,我不接受错误classes property
与定义我的风格function decorator
和一个空的对象; 但是,不可能只使用 style & className properties
<AppBar position="static" style={exampleStyleObj} className={exampleClass} >
我遵循使用withStyle
material-ui doc 中的装饰器的样式方法
CSS IN JS & Over
https://material-ui-next.com/customization/css-in-js/覆盖
https://material-ui-next.com/customization/overrides/Typescript Material-UI 指南
https://material-ui-next.com/guides/typescript/
我正在使用,typescript@2.4.0 material-ui@1.0.0-beta.21
我检查过是否有相同的问题并在其他地方回答,甚至检查了 material-ui 源代码,发现以下内容,
/**
* All standard components exposed by `material-ui` are `StyledComponents` with
* certain `classes`, on which one can also set a top-level `className` and inline
* `style`.
*/
export type StandardProps<C, ClassKey extends string, Removals extends keyof C = never> =
& Omit<C & { classes: any }, 'classes' | Removals>
& StyledComponentProps<ClassKey>
& {
className?: string;
style?: Partial<React.CSSProperties>;
}
这StandardProps
是最ComponentProps interface
扩展的(例如,AppBarProps)。但是到目前为止,我没有多少运气来解决这些问题。
请帮忙!