ric*_*i90 6 typescript material-ui
在尝试将我的 React 应用程序转换为打字稿时,我不断收到以下错误,并且我一生都无法弄清楚它在说什么。该应用程序在纯 JS 中运行良好。我在用着material-ui@next
TS2345: Argument of type 'typeof ApplicationMenu' is not assignable to parameter of type 'ComponentType<AppMenuProps & WithStyles<"root" | "flex" | "menuButton" | "appBar" | "loginButton">>'.
Type 'typeof ApplicationMenu' is not assignable to type 'StatelessComponent<AppMenuProps & WithStyles<"root" | "flex" | "menuButton" | "appBar" | "loginBu...'.
Type 'typeof ApplicationMenu' provides no match for the signature '(props: AppMenuProps & WithStyles<"root" | "flex" | "menuButton" | "appBar" | "loginButton"> & { children?: ReactNode; }, context?: any): ReactElement<any> | null'.
Run Code Online (Sandbox Code Playgroud)
产生此错误的代码如下
export interface AppMenuProps {
classes: Record<string, string | undefined>
}
const styles = (theme : Theme) => ({
root: {
width: '100%',
},
flex: {
flex: 1,
},
menuButton: {
marginLeft: -12,
marginRight: 20,
},
appBar: {
background: theme.palette.common.black,
color: theme.palette.common.white,
},
loginButton: {
color: theme.palette.common.white
}
});
class ApplicationMenu extends Component<AppMenuProps, {}> {
render() {
const {classes} = this.props;
return (
<div className={classes.root}>
<AppBar position="static" classes={{root: classes.appBar}}>
<Toolbar>
<IconButton className={classes.menuButton} color="primary" aria-label="Menu">
<MenuIcon/>
</IconButton>
<Typography type="title" color="inherit" className={classes.flex}>
Supportworks Dashboard
</Typography>
<Button classes={{root: classes.loginButton}}>Login</Button>
</Toolbar>
</AppBar>
</div>
);
}
}
export default withStyles(styles)(ApplicationMenu)
Run Code Online (Sandbox Code Playgroud)
请参阅 TypeScript 指南,特别是有关 withStyles 的部分(已编辑链接):
类组件稍微麻烦一些。由于当前 TypeScript 装饰器支持的限制,
withStyles无法用作类装饰器。相反,我们装饰一个类组件......
你需要做这样的事情:
import { WithStyles } from 'material-ui/styles';
export interface AppMenuProps {
classes: Record<string, string | undefined>
}
const styles = (theme : Theme) => ({
root: {
width: '100%',
},
flex: {
flex: 1,
},
menuButton: {
marginLeft: -12,
marginRight: 20,
},
appBar: {
background: theme.palette.common.black,
color: theme.palette.common.white,
},
loginButton: {
color: theme.palette.common.white
}
});
const ApplicationMenu = decorate(
class extends React.Component<AppMenuProps & WithStyles<'root' | 'flex' | 'menuButton' | 'appBar' | 'loginButton'>> {
render() {
// ...
}
}
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5983 次 |
| 最近记录: |