【发布时间】:2017-12-29 17:57:29
【问题描述】:
在尝试将我的 React 应用程序转换为 typescript 时,我不断收到下面的错误,我终其一生都无法弄清楚它在说什么。该应用程序在纯 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'.
产生此错误的代码如下
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)
【问题讨论】:
-
你用的是什么版本的打字稿?
-
根据 package.json 的版本 2.6.2